【问题标题】:How to extract text from specific div class using Beautiful Soup?如何使用 Beautiful Soup 从特定的 div 类中提取文本?
【发布时间】:2020-03-08 19:29:39
【问题描述】:

所以基本上我想提取里面的文字

<div class = "examplediv">
example text
</div>

我用过

url='https://sample.com'
headers={"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'}
page = requests.get(url, headers=headers)
soup = BeautifulSoup(page.content, 'html.parser')
data = soup.find("div", {"class":"examplediv"})
clean_data = data.text
print(clean_data)

但它永远不会在终端返回“无”。

我该怎么办?

【问题讨论】:

    标签: python html web-scraping beautifulsoup


    【解决方案1】:

    尝试find_all 而不是find,以防重复相同的类名,这样您可能只会得到第一项。 find_all 应该为您返回一个项目列表。否则你可能会在event 下处理class,它是通过JavaScript 加载的,不会通过requests 模块呈现。在这种情况下,您需要使用selenium,或者共享url,以便我们手动检查其他方法。

    【讨论】:

      猜你喜欢
      • 2014-08-06
      • 2021-01-02
      • 2013-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多