【问题标题】:How do i paste my scraped data into a python dictionary (csv?)如何将抓取的数据粘贴到 python 字典(csv?)
【发布时间】: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


    【解决方案1】:

    您没有向scraped_data 列表添加任何内容(空字典):

    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:
        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)
        scraped_data.append(
            {"bed_naam": bed_naam.strip(), "bed_price": bed_price.strip()}
        )
    
    dataFrame = pd.DataFrame.from_dict(scraped_data)
    dataFrame.to_csv("swisssense_details.csv", index=False)
    

    创建了这个数据框:

                                          bed_naam bed_price
    0                        Bedframe Balance Pure   1.080,-
    1                       Bedframe Balance Focus     990,-
    2              Gestoffeerd Bedframe Dream Moon     949,-
    3                         Bedframe Balance Raw   1.090,-
    4                      Bedframe Balance Tender   1.290,-
    5                      Bedframe Balance Gentle   1.240,-
    6   Gestoffeerd Bedframe Web-Only Dream Cosmos     279,-
    7                    Bedframe Balance Timeless   1.080,-
    8              Gestoffeerd Bedframe Dream Star     899,-
    9   Gestoffeerd Bedframe Web-Only Dream Galaxy     299,-
    10            Gestoffeerd Bedframe Dream Lunar     949,-
    11   Gestoffeerd Bedframe Web-Only Dream Comet     299,-
    12          Gestoffeerd Bedframe Dream Stellar     949,-
    

    截图:

    【讨论】:

    • 它是如何形成这样的结构的?我的电子表格都在一个单元格中.... tnx 提供帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-07
    • 2010-09-24
    • 1970-01-01
    • 2019-09-25
    • 2022-01-24
    • 2021-05-19
    • 1970-01-01
    相关资源
    最近更新 更多