为什么python输出与目标网站的html不匹配

你好1094

我正在尝试对目标网站进行网络抓取,例如价格,产品名称,产品的jpeg等详细信息,但是使用beautifulsoup通过python提取的内容似乎与目标网站中的html不匹配(使用F12)。

我试过在beautifulsoup函数中使用html.parser和lxml,但两者似乎没有什么区别。我已经尝试过搜索类似的问题,但没有发现任何问题。我正在使用Atom运行python代码,并正在使用Ubuntu 18.04.2。我在使用python方面还很陌生,但是之前已经进行了编码。

url = 'https://www.target.com/s?searchTerm=dove'
# Gets html from the given url
response = get(url)
html_soup = BeautifulSoup(response.text, 'html.parser')
items = html_soup.find_all('li', class_ = 'bkaxin')
print(len(items))

假设输出28,但我始终得到0

伊斯梅尔·帕迪利亚(Ismael Padilla)

您似乎要查找的元素似乎不存在,因为它们是在网站加载后动态创建的。您可以通过在网站首次加载时查看源代码来自己查看。您也可以尝试打印,html_soup.prettify()然后会发现您要查找的元素不存在。

这个问题的启发,我提出了一种基于的解决方案

from bs4 import BeautifulSoup
from selenium import webdriver

url = "https://www.target.com/s?searchTerm=dove"
driver = webdriver.Firefox()

driver.get(url)
html = driver.page_source
html_soup = BeautifulSoup(html, 'html.parser')
items = html_soup.find_all('li', class_ = 'bkaXIn')
driver.close()

print(len(items))

28当我运行它时,前面的代码输出

请注意,您需要安装selenium(此处安装指南)和适当的驱动程序才能工作(在我的解决方案中,我使用了Firefox驱动程序,可以在此处下载)。

另请注意,我在中使用class_ = 'bkaXIn'(区分大小写!)html_soup.find_all

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章