【发布时间】:2021-03-28 10:45:24
【问题描述】:
我是这方面的初学者,所以我尝试从 youtube 和堆栈溢出中学习。我现在卡住了。
我正在使用 python 抓取工具抓取网站。 现在我想使用 Python 将刮板的结果放入字典中。我选择 .csv 以便我可以轻松地在我的网站中构建某种类型的搜索功能,以便人们可以搜索 .csv 并且网站会向他们显示他们的搜索结果。我已经有了创建 .csv 的东西,只是当我运行它时里面什么都没有.....任何
import requests
from bs4 import BeautifulSoup
import pandas as pd
scraped_data=[]
details= {}
page=requests.get('https://www.swisssense.nl/bedden')
soup = BeautifulSoup(page.content, 'html.parser')
products = soup.find_all("a", class_="product-item-link")
prices = soup.find_all("span", class_="price")
images = soup.find_all("img", class_="product-image-photo")
bed_data = soup.find_all('li', attrs={'class', 'item product product-item'})
for bed in bed_data:
swisssense_details = {}
bed_naam = bed.find("a", class_="product-item-link").getText()
bed_price = bed.find("span", class_="price").getText() # print(bed_naam.text, bed_price.text)
print(bed_naam, bed_price)
scraped_data.append(swisssense_details)
dataFrame = pd.DataFrame.from_dict(scraped_data)
dataFrame.to_csv('swisssense_details.csv', index=False)
【问题讨论】:
标签: python-3.x pandas dataframe csv