![](/img/trans.png)
[英]How to find parent element by class name using BeautifulSoup?
[英]How to find an html class name using BeautifulSoup?
我這樣寫我的代碼:
html_source = driver.page_source
soup = BeautifulSoup(html_source, 'lxml')
feed_root= soup.find('div', id="feed-root")
oneday_today = feed_root.find_all('div', class_="_1950o")
grades_today = feed_root.find_all('div', class_="_29Z79")
for oneday in oneday_today:
print(oneday.text.split())
for grades in grades_today:
print(grades.text.split())
我想得到這樣的結果:
class_="_1950o" SOME_text
class_="_29Z79" SOME_text
class_="_29Z79" SOME_text
class_="_29Z79" SOME_text
class_="_1950o" SOME_text
class_="_29Z79" SOME_text
class_="_29Z79" SOME_text
class_="_29Z79" SOME_text
您可以使用soup['attribute']
獲取屬性,或者在您的情況下, ['class']
這應該有效:
for oneday in oneday_today:
print(oneday['class'], oneday.text.split())
for grades in grades_today:
print(oneday['grades'], grades.text.split())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.