html - 使用 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')
但只得到 [ ] )
解决方案
您正在寻找的数据可能无法通过课程看到,但您可以手动搜索产品标题,以便您可以在脚本标签中找到
text=soup.find_all("script")[5].contents[0]
re
使用上述代码后,我们可以使用模块提取文本
import re
main_data=re.findall(r'\{.*?\}', text)
在其中main_data
作为字典列表返回,您可以提取您想要的任何数据
推荐阅读
- javascript - 如何将数组值传递给函数javascript
- javascript - Nock 不拦截对第 3 方的呼叫
- angular - 具有父子路由的部分反应式表单
- javascript - 按列名在单元格中插入值
- performance - 在查询按日期字符串分区的配置单元表时,使用 between vs '> & <' 有区别吗?
- javascript - 页面可以在 iPhone 中向下拖动 - Framework7 Cordova
- php - 500 从查询中获取大量记录时发生内部错误
- python - 想要运行只打印一次 else 语句的 PRAW 机器人
- python - 为什么我需要 web api 来链接 django 和其他 js 框架
- django - Wagtail 博客示例导致没有属性“_default_manager”