【问题标题】:How do i select the 87% on this page using Python and BeautifulSoup如何使用 Python 和 BeautifulSoup 在此页面上选择 87%
【发布时间】:2019-09-12 00:16:22
【问题描述】:

我正在尝试从该页面上抓取一些数据。

https://www.australiacouncil.gov.au/research/electorate-profiles/wright/#!Wright

问题是我无法使用 BeautifulSoup 和 Python 获得 87%。

当我跑步时:

soup.find("div",{"class":"progress-bar-item progress-bar-item--text "})

我得到的是 {{perc}} 而不是实际数字。

【问题讨论】:

  • 这是因为“87%”可能是通过 java 脚本在您的机器上(相对于服务器上)实时计算的。您可能想考虑使用selenium
  • 谢谢,我想可能是这样,但我想我最好先检查一下蜂巢思维。

标签: python html web-scraping beautifulsoup python-requests


【解决方案1】:

它看起来确实是动态构造的。如果沿着 selenium 路线走,您可以执行以下操作:

from selenium import webdriver

d = webdriver.Chrome(r'path\chromedriver.exe')
d.get('https://www.australiacouncil.gov.au/research/electorate-profiles/wright/#!Wright')
print(d.find_element_by_css_selector('.dzs-progress-bar strong').text)
d.quit()

【讨论】:

  • 辛苦了,谢谢。我添加了一点时间延迟以确保所有元素都已加载,并且因为我已经编写了一堆 BeautifulSoup 逻辑,所以我最终使用 html = d.page_source 并将其解析为 BeautifulSoup。
猜你喜欢
  • 2015-10-21
  • 2015-06-07
  • 1970-01-01
  • 2013-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-19
  • 2020-07-01
相关资源
最近更新 更多