我正在尝试创建一个字典,但我希望它的值部分是一个集合。我想把这些值放到一个集合中每次我循环的时候,例如,{key: value} ->{key: value, value, value}, {key: value, value}下面是我的代码
from bs4 import BeautifulSoup
import urllib
import re
def linkTexts(url):
url = urllib.request.urlopen(url).read()
soup = BeautifulSoup(url,'lxml')
q2Dictionary = {}
links = soup.find_all('a', href = True)
for link in links:
linkUrl = link["href"]
if 'http' in linkUrl:
linkText = link.get_text().strip()
if linkUrl not in q2Dictionary.keys():
q2Dictionary[linkUrl] = linkText
if linkUrl in q2Dictionary.keys():
q2Dictionary.update({linkUrl: linkText})
使用空集dict.setdefault()
:
q2Dictionary.setdefault(linkUrl, set()).add(linkText)
这相当于:
if linkUrl not in q2Dictionary:
q2Dictionary[linkUrl] = set()
q2Dictionary[linkUrl].add(linkText)
而不是创建字符串字典。为什么不创建一个字符串列表的字典呢?
from bs4 import BeautifulSoup
import urllib
import re
def linkTexts(url):
url = urllib.request.urlopen(url).read()
soup = BeautifulSoup(url,'lxml')
q2Dictionary = {}
links = soup.find_all('a', href = True)
for link in links:
linkUrl = link["href"]
if 'http' in linkUrl:
linkText = link.get_text().strip()
if linkUrl not in q2Dictionary.keys():
q2Dictionary[linkUrl] = [linkText]
else:
q2Dictionary[linkUrl].append(linkText)
使用linkText初始化每个键,并将具有相同键的linkText附加到现有列表中。