首页 > 解决方案 > 使用 BeautifulSoup 我找不到一些元素

问题描述

来自网站的图像我试图通过运行此脚本来获取“col-12 col-md-3 product-grid-item-container render-enhanced”下的内容

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'

}
r = requests.get('https://www.sneaksup.com/search?q=dunk&pagenumber=1', headers=headers)
soup = BeautifulSoup(r.content, 'lxml')
hrefs = soup.find('div', class_='product-list-inner-container bg-white')
print(hrefs)

但不幸的是,我得到的只是这个。如何获取“col-12 col-md-3 product-grid-item-container rendering-enhanced”上方的所有信息(我试图从以下位置找到:

hrefs = soup.find('div', class_='col-12 col-md-3 product-grid-item-container rendered-enhanced')

但只得到 [ ] )

标签: htmlbeautifulsoupelement

解决方案


您正在寻找的数据可能无法通过课程看到,但您可以手动搜索产品标题,以便您可以在脚本标签中找到

text=soup.find_all("script")[5].contents[0]

re使用上述代码后,我们可以使用模块提取文本

import re
main_data=re.findall(r'\{.*?\}', text)

在其中main_data作为字典列表返回,您可以提取您想要的任何数据


推荐阅读