在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).