我想在我的Flask-App中使用bs4搜索特定范围。
我以前从未使用过bs4,所以我有点困惑为什么我的搜索没有任何结果。
from bs4 import BeautifulSoup
url = "https://www.mcfit.com/de/fitnessstudios/studiosuche/studiodetails/studio/berlin-lichtenberg/"
html_content = requests.get(url).text
soup = BeautifulSoup(html_content, "lxml")
spans = soup.find_all('span', {'class': 'sc-fzoXWK hnKkAN'})
print(spans)
“ sc-fzoXWK hnKkAN”类仅包含1个跨度。当我执行时,我只会得到一个[]
结果。
这些内容是使用javascript动态生成的,因此使用请求检索HTML只会检索静态内容,您可以将BeautifulSoup与Selenium之类的东西结合使用以实现所需的内容:
安装硒:
pip install selenium
然后使用Firefox引擎或任何其他支持javascript的内容检索内容:
from bs4 import BeautifulSoup
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('https://www.mcfit.com/de/fitnessstudios/studiosuche/studiodetails/studio/berlin-lichtenberg/')
html_content = driver.page_source
soup = BeautifulSoup(html_content, "lxml")
elems = soup.find_all('div', {'class': 'sc-fzoXWK hnKkAN'})
print(elems)
如果您使用的是Firefox,则您的脚本需要可以访问geckodriver,您可以从https://github.com/mozilla/geckodriver/releases下载该文件,并将其放入PATH(如果使用的是c:/ windows,操作系统),因此可以在任何地方使用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句