在FOR循环中将值添加到字典中.更新而不是“附加". [英] Adding values to dictionary in FOR loop. Updating instead of "Appending"

查看:170
本文介绍了在FOR循环中将值添加到字典中.更新而不是“附加".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import requests
from bs4 import BeautifulSoup

urls = ['url1']
dictionary = {}

for url in urls:
    req = requests.get(url)
    soup = BeautifulSoup(req.text, "lxml")

    for sub_heading in soup.find_all('h3'):  
        dictionary[url] = sub_heading.text

print(dictionary)

我得到的结果看起来像这样{url:sub_heading.text},而不是得到包含我期望的所有值的字典. 似乎循环正在更新而不是追加" ...

I'm getting a result that looks like this {url : sub_heading.text} instead of getting a dictionary containing all the values I'm expecting. It seems that the loop is updating instead of "appending"...

推荐答案

Python字典具有key:value对,并且不能有重复的key.

Python Dictionaries have key:value pairs, and it can not have duplicate keys.

因此在此代码中,"url"是 key ,而"sub_heading.text"是 value .

So in this code, 'url' is key and 'sub_heading.text' is value.

每次循环运行时,字典中只有'url'的值是 不断更新.

And everytime this loop runs, only the value for 'url' in dict is getting updated.

 for sub_heading in soup.find_all('h3'):  
        dictionary[url] = sub_heading.text

您应该使用其他数据结构代替Dict(例如,元组列表或数据框列表).

You should use some other data structure instead of Dict (for e.g. list of tuples or dataframe).

这篇关于在FOR循环中将值添加到字典中.更新而不是“附加".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆