【问题标题】:How can I get a BeautifulSoup object from BeautifulSoup.find()?如何从 BeautifulSoup.find() 获取 BeautifulSoup 对象?
【发布时间】:2021-06-21 06:46:58
【问题描述】:

这可能是一个新手问题,但是我已经阅读了整个 bs4 文档并且正在努力寻找解决我的问题的方法。

我基本上有一个 BeautifulSoup 对象,我从中调用 .find() 来定位某个 div,然后我想在这个 div 上调用 .find_all() 。 我尝试过这样的事情:

soup = bs4.BeautifulSoup(browser.page_source, "lxml")

competition = soup.find("div", class_="accordion-competition__content_main").contents

events = competition.find_all("div", class_="sport-event-card")

for event in competition:

    print("EVENT: ", event)

无济于事。

如何使用 BS4 过滤已过滤的部分?任何帮助是极大的赞赏。我一直在这个问题上磕磕绊绊,但还没有找到合适的解决方案来过滤一个部分。

【问题讨论】:

    标签: python web-scraping beautifulsoup


    【解决方案1】:
    soup = bs4.BeautifulSoup(browser.page_source, "lxml")
    
    competition = soup.find("div", {"class": "accordion-competition__content_main"}) # get competition node
    
    events = competition.find_all("div", {"class": "sport-event-card"}) # find inside competition node
    
    for event in events:
        print("EVENT: ", event)
    

    【讨论】:

      猜你喜欢
      • 2019-07-15
      • 2021-06-18
      • 1970-01-01
      • 2021-07-20
      • 2022-12-15
      • 2011-01-03
      • 1970-01-01
      • 1970-01-01
      • 2010-11-17
      相关资源
      最近更新 更多