【问题标题】:Webscrape prices by extracting values inside div class with Beautiful Soup, Selenium, and Pandas通过使用 Beautiful Soup、Selenium 和 Pandas 提取 div 类中的值来获取 Web 价格
【发布时间】:2019-09-28 19:23:34
【问题描述】:

我正在尝试返回给定尺寸的产品价格,因为它们每天都在波动。 我能够让我的代码在使用“类”的网站上运行,但我无法让它与 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: []

【问题讨论】:

    标签: python pandas selenium web-scraping beautifulsoup


    【解决方案1】:

    由于该行,您将获得 EMPTY 列表。

    for a in soup.findAll('div',href=True, attrs={'class':'product-essential row-fluid product-type-configurable'}):
    

    div 标签中没有href 属性。

    把这个改成:

    for a in soup.findAll('div',attrs={'class':'product-essential row-fluid product-type-configurable'}):
    

    【讨论】:

      猜你喜欢
      • 2021-02-23
      • 2020-09-03
      • 1970-01-01
      • 1970-01-01
      • 2011-01-09
      • 1970-01-01
      • 1970-01-01
      • 2019-09-01
      • 2013-01-09
      相关资源
      最近更新 更多