【问题标题】:How do I extract text from a button using Beautiful Soup?如何使用 Beautiful Soup 从按钮中提取文本?
【发布时间】:2021-02-02 01:09:00
【问题描述】:

我正在尝试抓取 GoFundMe 信息,但似乎无法提取捐赠者的数量。

这是我要导航的 html。我正在尝试检索 11.1K,

<ul class="list-unstyled m-meta-list m-meta-list--default">
  <li class="m-meta-list-item">
    <button class="text-stat disp-inline text-left a-button a-button--inline" data-element- 
    id="btn_donors" type="button" data-analytic-event-listener="true">
      <span class="text-stat-value text-underline">11.1K</span>&nbsp;
        <span class="m-social-stat-item-title text-stat-title">donors</span>

我尝试过使用

donors = soup.find_all('li', class_ = 'm-meta-list-item')
for donor in donors:
  print(donor.text)

类/按钮似乎隐藏在另一个类中?如何提取它?

我是 beautifulsoup 的新手,但使用过很多硒。

提前致谢。

【问题讨论】:

标签: python web-scraping beautifulsoup web-crawler


【解决方案1】:

这些筹款活动页面都具有相似的 html,并且该值是动态检索的。我建议使用 selenium 和 css 类选择器

from selenium import webdriver

d = webdriver.Chrome()
d.get('https://www.gofundme.com/f/treatmentforsiyona?qid=7375740208a5ee878a70349c8b74c5a6')
num = d.find_element_by_css_selector('.text-stat-value').text
print(num)
d.quit()

了解有关硒的更多信息:

https://sqa.stackexchange.com/a/27856

【讨论】:

  • 嗨!在这种情况下或您的线路中,css 选择器对我不起作用。我试图使用漂亮的汤,但如果你可以编辑你的 CSS 选择器来工作,我会接受它作为答案。
  • 请解释一下您所说的没用是什么意思?任何错误信息?我在几个不同的页面上测试了上述内容,包括您提供的页面,所以我只能认为也许我需要添加一个等待条件以防您遇到超时。
  • web 元素存在,但没有返回文本。没有错误。
  • 如果你在元素更新之前访问,如果你先短暂睡眠会发生什么?
【解决方案2】:

获取 id gofundme.com/f/{THEID} 并调用 API

/web-gateway/v1/feed/THEID/donations?sort=recent&limit=20&offset=20

处理数据

for people in apiResponse['references']['donations']
    print(people['name'])

使用浏览器控制台查找主机 API。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-23
    • 1970-01-01
    • 2021-04-20
    • 2022-01-17
    • 2021-04-07
    相关资源
    最近更新 更多