【问题标题】:Appended list with dictionaries has the same value. BeatifulSoup data带有字典的附加列表具有相同的值。 BeautifulSoup 数据
【发布时间】:2020-07-12 16:57:51
【问题描述】:

我想从一个网站中提取数据并建立一个包含字典的列表。

item = {"price": "", "price per gramm": ""}
list_of_sku = []


soup = BeautifulSoup(open("./FULL_data.html"), "html.parser")

for divs in soup.find_all('div', attrs={"class": "col-xs-6 col-sm-4"})[:2]:
    links = divs.find_all("tr")
    for row in links:
        # We get list of prices here
        item_text = row.find('td')
        if item_text:
            item["price"] = str(item_text.text)
            print(item["price"])
            list_of_sku.append(item)
    print(list_of_sku)

结果,我得到了一个附加列表,但是一行中的所有价格都是一样的。

/PycharmProjects/MyFirstOne/WEBSCRAPING/Work with Soup data.py" 

$125.70   [{'price': '$125.70', 'price per gramm': ''}]  

$35.70  [{'price': '$35.70', 'price per gramm': ''}, {'price': '$35.70', 'price per gramm': ''}]

Process finished with exit code 0

请帮我解决这个问题。

【问题讨论】:

标签: python-3.x list beautifulsoup append


【解决方案1】:

看起来您每次都在修改“项目”对象并重新插入它。尝试将第一行移到循环中:


list_of_sku = []

with open('./FULL_data.html', 'r') as f:
    data = f.read()

soup = BeautifulSoup(open("./FULL_data.html"), "html.parser")

for divs in soup.find_all('div', attrs={"class": "col-xs-6 col-sm-4"})[:2]:
    links = divs.find_all("tr")
    for row in links:
        # We get list of prices here
        item_text = row.find('td')
        if item_text:
            item = {"price": "", "price per gramm": ""}
            item["price"] = str(item_text.text)
            print(item["price"])
            list_of_sku.append(item)
    print(list_of_sku)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    • 1970-01-01
    • 2021-05-09
    • 1970-01-01
    • 1970-01-01
    • 2019-06-09
    • 2019-12-26
    相关资源
    最近更新 更多