嵌套于两个列表的循环python,以生成列表以进入熊猫数据框表
我有两个列表: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'}....
我不确定如何迭代价格数据。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Zip
名称和价格,然后为每对名称和价格创建一个新的dact:zip
names and prices and then create a new dict for each pair of name and price:除了回答@PSIDOM - 尝试避免使用一堆列表,因为如果没有元素,您只能在困难中控制长度。
更改收集数据的策略,您可以一口气获得信息。
示例
输出
...
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.
Example
Output
...