簡體   English   中英

如何使用 BeautifulSoup 找到 html class 名稱?

[英]How to find an html class name using BeautifulSoup?

html 代碼在這里

我這樣寫我的代碼:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM