首页 > 解决方案 > 如何使用 Python 和 BeautifulSoup 在此页面上选择 87%

问题描述

我正在尝试从此页面中抓取一些数据。

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}} 而不是实际数字。

标签: pythonhtmlweb-scrapingbeautifulsouppython-requests

解决方案


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

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()

推荐阅读