繁体   English   中英

BeautifulSoup 找不到 class 的 HTML 元素

[英]BeautifulSoup can't find HTML element by class

这是我试图用 Python 抓取的网站:

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

我想使用“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 来刮取你想要的数据

Python beautifulsoup find_all 找不到<div class="“" ”></div><div id="text_translate"><p>我正在尝试使用 beautifulsoup 来查找 HTML 标签中的内容。 但是当标签是/ <strong>div class=" "</strong> /时,就不行了。 双引号中有<strong>空格</strong>时不能正确识别。</p><p> 这是我的代码:</p><pre> from bs4 import BeautifulSoup if __name__ == "__main__": soup = BeautifulSoup(open("1946.html", encoding='utf-8'), 'lxml') for k in (soup.find_all('div', class_=" ")): print(k)</pre><p> 谢谢你的帮助。</p></div>

[英]Python beautifulsoup find_all can‘t find <div class=“ ”>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM