[英]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')
})
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)
... ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.