python - 通过使用 Beautiful Soup、Selenium 和 Pandas 提取 div 类中的值来抓取价格
问题描述
我试图获得给定尺寸的产品价格,因为它们每天都在波动。我能够让我的代码在使用“类”的网站上工作,但我无法让它与 div 和 span 类一起工作。
链接:https ://www.flightclub.com/supreme-x-dunk-sb-low-varsity-red-varsity-red-white-black-152127?size=9.5 价格:550 美元(截至本帖)
from selenium import webdriver
from bs4 import BeautifulSoup
import pandas as pd
driver = webdriver.Chrome("/Users/donlento7/chromedriver")
products=[] #List to store name of the product
prices=[] #List to store price of the product
driver.get('https://www.flightclub.com/supreme-x-dunk-sb-low-varsity-red-varsity-red-white-black-152127?size=9.5')
content = driver.page_source
soup = BeautifulSoup(content, "lxml")
for a in soup.findAll('div',href=True, attrs={'class':'product-essential row-fluid product-type-configurable'}):
name=a.find('div', attrs={'class':'mb-padding'})
price=a.find('span', attrs={'class':'price'})
products.append(name.text)
prices.append(price.text)
df = pd.DataFrame({'Product Name':products,'Price':prices})
#df.to_csv('products.csv', index=False, encoding='utf-8')
print(df)
输出:
Empty DataFrame
Columns: [Product Name, Price]
Index: []
解决方案
由于该行,您将获得 EMPTY 列表。
for a in soup.findAll('div',href=True, attrs={'class':'product-essential row-fluid product-type-configurable'}):
标签中没有href
属性。div
将此更改为:
for a in soup.findAll('div',attrs={'class':'product-essential row-fluid product-type-configurable'}):
推荐阅读
- mysql - MySQL 如何从缓冲池中逐出页面?
- node.js - 如何将套接字连接到服务器?
- amazon-athena - HIVE_METASTORE_ERROR 预期为“STRING”,但找到了“STRING”
- jquery - 使用 sessionStorage 的 JQuery 函数不适用于 .aspx 文件
- mongodb - 如何在 mongodb 过滤器中使用计算日期?
- javascript - 如何根据内部子数组的第一项合并二维数字数组?
- c# - 如何获取给定月份生日表上的用户列表?
- swift - 将 do catch 块放入数组
- ios - Swift 将按钮添加到 UISearchBar / UITableView
- android - 如何在活动中隐藏android片段以使当时只有一个片段处于活动状态?