shinma
@shinma
ИТ, Linux, Mainframe, Unix, Кластер

Python. BeautifulSoup. Как перебрать все элементы?

Подскажите плиз как в print вывести все значения
class="FPmhX notranslate TlrDj" title=" вот это надо "
и текст span
то есть просто принтов вывести по очереди все значения title \n + текст span
Я не понимаю как сделать for to do, while или как тут положено :(

5ca219ce86dd2603902302.png
#http://qaru.site/questions/16889278/beautiful-soup-find-is-returning-none
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import requests, re, json
import codecs
import sys
import logging
logging.basicConfig(format = '%(asctime)s : %(levelname)s : %(message)s', level = logging.DEBUG)
# -*- coding: utf-8 -*-
def main():
    options = Options()
    options.add_argument("--headless")
    driver = webdriver.Chrome(
        options=options, executable_path="C:\Python37\Laba\chromedriver.exe"
    )
    try:
        driver.get("https://www.instagram.com/p/BvWM5KOnaQl/")
        soup = BeautifulSoup(driver.page_source, "lxml")
        class_k59kT=soup.find("ul", {"class": "k59kT"})
        print(str(class_k59kT.find('h3').a.get('title')))
    finally:
        driver.quit()
if __name__ == "__main__":
    main()
  • Вопрос задан
  • 1406 просмотров
Решения вопроса 1
datka
@datka
from bs4 import BeautifulSoup

soup = BeautifulSoup("""
	<li class="K823gt" role="menuitem">
		<span> text 1 </span>
	</li>
	<li class="x65HGs" role="menuitem">
		<span> text 2 </span>
	</li>	
	<li class="OPZe6g" role="menuitem">
	<span> text 3 </span>
	</li>
	""", "html.parser")

span_data = soup.find_all("li", {"role":"menuitem"})

for x in span_data:
	x.find("span")
	print(x.text)
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы