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

发布于 2025-02-12 23:54:47 字数 1214 浏览 1 评论 0 原文

我有两个列表: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}...]

我想以以下格式创建一个表,以便我可以将其插入 pandas dataframe表:

{'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}....

到目前为止我使用 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)

输出是:

{'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 have two lists: SoapName and SoapPrices as follows each containing 16 elements:

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:

{'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:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑看君怀她人 2025-02-19 23:54:47

Zip 名称和价格,然后为每对名称和价格创建一个新的dact:

[{**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}]

zip names and prices and then create a new dict for each pair of name and price:

[{**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}]
做个ˇ局外人 2025-02-19 23:54:47

除了回答@PSIDOM - 尝试避免使用一堆列表,因为如果没有元素,您只能在困难中控制长度。

更改收集数据的策略,您可以一口气获得信息。

for e in soup.select('article'):
    data.append({
        'title':e.h1.text,
        'price':e.bdi.span.next_sibling,
        'url':e.a.get('href')
    })
示例
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)
输出
标题 价格 URL
啤酒和蜂蜜洗发水条 6.00 https:// stylather.co.uk/shop/beer-and-honey-shampoo-bar/
Cedarwood剃须肥皂 5.00 https://ratherlather.co.uk/shop/shop/cedarwood-shaving-shaving-soap/
洋甘菊和薰衣草洗发水和身体棒 5.00 https://ratherlather.co.uk/shop/shop/chamomile - 和lavender-shampoo-and-body-bar/
fir针,薄荷洗发水和身体棒 5.00 https://ratherlather.co.uk/shop /fir-deedle and mint-shampoo and-body-bar/
薄荷巧克力脸和身体棒 5.00 https://ratherlather.co.uk/shop/shop/mint-chocelice -face-and-Body-bar/
薄荷巧克力脸和身体Bar-纯素食 5.00 https://ratherlather.co.uk/shop/mint - 巧克力 - 面棒式素食/
橙色和蜂蜜洗发水和身体棒 5.00 https://ratherlather.co.uk/shop/orange - honey-shampoo and-body-bar/
迷迭香,柠檬草和薰衣草脸和身体棒 5.00
身体黄油 - 黄油up&定居 7.00 https://ratherlather.co.uk/商店/身体燃料 - 燃料固定/

...

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.

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 https://ratherlather.co.uk/shop/beer-and-honey-shampoo-bar/
Cedarwood Shaving Soap 5.00 https://ratherlather.co.uk/shop/cedarwood-shaving-soap/
Chamomile and Lavender Shampoo and Body Bar 5.00 https://ratherlather.co.uk/shop/chamomile-and-lavender-shampoo-and-body-bar/
Fir Needle and Mint Shampoo and Body Bar 5.00 https://ratherlather.co.uk/shop/fir-needle-and-mint-shampoo-and-body-bar/
Mint Chocolate Face and Body Bar 5.00 https://ratherlather.co.uk/shop/mint-chocolate-face-and-body-bar/
Mint Chocolate Face and Body Bar- Vegan 5.00 https://ratherlather.co.uk/shop/mint-chocolate-face-and-body-bar-vegan/
Orange and Honey Shampoo and Body Bar 5.00 https://ratherlather.co.uk/shop/orange-and-honey-shampoo-and-body-bar/
Rosemary, Lemongrass and Lavender Face and Body Bar 5.00 https://ratherlather.co.uk/shop/rosemary-lemongrass-lavender-face-body-bar/
Body Butter - Butter Up & Settle Down 7.00 https://ratherlather.co.uk/shop/body-butter-butter-up-settle-down/

...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文