简体   繁体   English

嵌套两个列表的for循环python以生成进入熊猫数据框表的列表

[英]nested for loop python of two lists to produce list to go into panda dataframe table

I have two lists: SoapName and SoapPrices as follows each containing 16 elements:我有两个列表: SoapName 和 SoapPrices 如下,每个列表包含 16 个元素:

SoapName肥皂名

[{'title': 'Beer and Honey Shampoo bar'},
 {'title': 'Cedarwood Shaving Soap'},
 {'title': 'Chamomile and Lavender Shampoo and Body Bar'}...]

SoapPrices肥皂价格

[{'price': 6.0},
 {'price': 5.0},
 {'price': 5.0}...]

I would like to create a table in the following format so that I can insert it into a pandas dataframe table:我想创建一个以下格式的表,以便我可以将它插入到pandas数据框表中:

{'title': 'Beer and Honey Shampoo bar', 'price': 6.0}
{'title': 'Cedarwood Shaving Soap', 'price': 5.0}
{'title': 'Chamomile and Lavender Shampoo and Body Bar', 'price': 5.0}....

So far I have gone back to first principles using BeautifulSoup to web scrape the data and tried to do a nested loop:到目前为止,我已经回到了使用BeautifulSoup网络抓取数据并尝试执行嵌套循环的基本原则:

all_soap = []

soapinformation = soup.find_all("h1", class_= "product_title entry-title elementor-heading-title elementor-size-default")

for soap in soapinformation:
    soapTitle = soap.find("a").text
    soapInfo = {"title" : soapTitle, "price" : price3[0-17]}
    print(soapInfo)

the output being:输出是:

{'title': 'Beer and Honey Shampoo bar', 'price': '6.00'}
{'title': 'Cedarwood Shaving Soap', 'price': '6.00'}
{'title': 'Chamomile and Lavender Shampoo and Body Bar', 'price': '6.00'}....

I'm not sure how to iterate the data for price.我不确定如何迭代价格数据。

zip names and prices and then create a new dict for each pair of name and price: zip名称和价格,然后为每对名称和价格创建一个新的字典:

[{**name, **price} for name, price in zip(names, prices)]

#[{'title': 'Beer and Honey Shampoo bar', 'price': 6.0}, 
# {'title': 'Cedarwood Shaving Soap', 'price': 5.0}, 
# {'title': 'Chamomile and Lavender Shampoo and Body Bar', 'price': 5.0}]

Just in addition to answer of @Psidom - Try to avoid working with a bunch of lists, cause you can control its length only with difficulty, if there are elements not available.除了@Psidom 的回答之外 - 尽量避免使用一堆列表,因为如果没有可用的元素,您只能很难控制它的长度。

Change your strategy collecting your data, you could get the infos in one go.改变你收集数据的策略,你可以一口气得到信息。

for e in soup.select('article'):
    data.append({
        'title':e.h1.text,
        'price':e.bdi.span.next_sibling,
        'url':e.a.get('href')
    })
Example例子
import pandas as pd
from bs4 import BeautifulSoup
import requests

url = 'https://ratherlather.co.uk/shop/'
req = requests.get(url)

soup = BeautifulSoup(req.content)
data = []
for e in soup.select('article'):
    data.append({
        'title':e.h1.text,
        'price':e.bdi.span.next_sibling,
        'url':e.a.get('href')
    })

pd.DataFrame(data)
Output输出
title标题 price价格 url网址
Beer and Honey Shampoo bar啤酒和蜂蜜洗发水吧 6.00 6.00 https://ratherlather.co.uk/shop/beer-and-honey-shampoo-bar/ https://ratherlather.co.uk/shop/beer-and-honey-shampoo-bar/
Cedarwood Shaving Soap雪松剃须皂 5.00 5.00 https://ratherlather.co.uk/shop/cedarwood-shaving-soap/ https://ratherlather.co.uk/shop/cedarwood-shaving-soap/
Chamomile and Lavender Shampoo and Body Bar洋甘菊和薰衣草洗发水和沐浴露 5.00 5.00 https://ratherlather.co.uk/shop/chamomile-and-lavender-shampoo-and-body-bar/ https://ratherlather.co.uk/shop/chamomile-and-lavender-shampoo-and-body-bar/
Fir Needle and Mint Shampoo and Body Bar冷杉针薄荷洗发水和沐浴露 5.00 5.00 https://ratherlather.co.uk/shop/fir-needle-and-mint-shampoo-and-body-bar/ https://ratherlather.co.uk/shop/fir-needle-and-mint-shampoo-and-body-bar/
Mint Chocolate Face and Body Bar薄荷巧克力面部和身体棒 5.00 5.00 https://ratherlather.co.uk/shop/mint-chocolate-face-and-body-bar/ https://ratherlather.co.uk/shop/mint-chocolate-face-and-body-bar/
Mint Chocolate Face and Body Bar- Vegan薄荷巧克力面部和身体棒 - 素食主义者 5.00 5.00 https://ratherlather.co.uk/shop/mint-chocolate-face-and-body-bar-vegan/ https://ratherlather.co.uk/shop/mint-chocolate-face-and-body-bar-vegan/
Orange and Honey Shampoo and Body Bar橙色和蜂蜜洗发水和身体棒 5.00 5.00 https://ratherlather.co.uk/shop/orange-and-honey-shampoo-and-body-bar/ https://ratherlather.co.uk/shop/orange-and-honey-shampoo-and-body-bar/
Rosemary, Lemongrass and Lavender Face and Body Bar迷迭香、柠檬草和薰衣草面部和身体棒 5.00 5.00 https://ratherlather.co.uk/shop/rosemary-lemongrass-lavender-face-body-bar/ https://ratherlather.co.uk/shop/rosemary-lemongrass-lavender-face-body-bar/
Body Butter - Butter Up & Settle Down身体黄油 - 涂抹和安定下来 7.00 7.00 https://ratherlather.co.uk/shop/body-butter-butter-up-settle-down/ https://ratherlather.co.uk/shop/body-butter-butter-up-settle-down/

... ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM