我是 python 的新手,我正在尝试从购物网站上抓取有关计算机的信息。尽管代码看起来没问题(没有错误),但在过程的最后它不会给我 csv 文件。我已经更改了课程,但没有任何效果。我在另一个网站上使用了这段代码,效果很好。另一个小问题:如何抓取多页结果。提前致谢!
from bs4 import BeautifulSoup
import pandas as pd
from selenium import webdriver
import pandas as pd
products=[] #List to store name of the product
prices=[] #List to store price of the product
fretes=[] #List to store rating of the product
driver = webdriver.Chrome(executable_path = r'C:\Users\gomes\Desktop\chromedriver.exe')
driver.get("https://lista.mercadolivre.com.br/macbook")
content = driver.page_source
soup = BeautifulSoup(content, 'html.parser')
for a in soup.findAll('li',href=True, attrs={'class':'results-item'}):
name=a.find('span', attrs={'class':'main-title'})
price=a.find('span', attrs={'class':'price__fraction'})
frete=a.find('span', attrs={'class':'text-shipping'})
products.append(name.text)
prices.append(price.text)
fretes.append(frete.text)
data = dict({'Product Name': products,
'Price': prices,
'Frete': fretes
})
# create dataframe
products_df = pd.DataFrame(
dict([(k, pd.Series(v)) for k, v in data.items()])
)
products_df.to_csv("C:\\Users\\gomes\\Desktop\\mac.csv")
偶然的你
相关分类