[英]BeautifulSoup can't find HTML element by class
这是我试图用 Python 抓取的网站:
我想使用“srp-results srp-list clearfix”的 class 访问“ul”元素。 这是我对请求和 BeautifulSoup 的尝试:
from bs4 import BeautifulSoup
import requests
url = 'https://www.ebay.de/sch/i.html?_from=R40&_nkw=iphone+8&_sacat=0&LH_Sold=1&LH_Complete=1&rt=nc&LH_ItemCondition=3000'
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
uls = soup.find_all('ul', attrs = {'class': 'srp-results srp-list clearfix'})
而 output 始终是一个空字符串。 我还尝试使用 Selenium Webdriver 抓取网站,得到了相同的结果。
首先,我对您的错误有点困惑,但经过一些调试后,我发现: eBay 使用 JavaScript 动态生成该 ul
因此,由于您无法使用 BeautifulSoup 执行 JavaScript 您必须使用 selenium 并等到 Z68615595AF75A60EDD8 加载 C
It is probably because the content you are looking for is rendered by JavaScript After the page loads on a web browser this means that the web browser load that content after running javascript which you cannot get with requests.get request from python.
我建议学习 Selenium 来刮取你想要的数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.