如何迭代,以便在dictionary(Python)中添加唯一值



这是我试图使用请求从一个网站获取数据并保存在字典调用的表中的代码,但当我试图迭代这些值并将其保存在列表中时,我遇到了以下错误,请提供任何帮助。

import requests
from bs4 import BeautifulSoup
list1 = []
table = {}
r = requests.get("https://www.century21.com/real-estate/rock-springs-wy/LCWYROCKSPRINGS/?k=1")
content =  r.content
soup = BeautifulSoup(content,'html.parser')
all = soup.find_all('div',{"class":"property-card-primary-info"})
for item in all:
print(item.find('a',{"class":"listing-price"}).text.replace('n','').replace('  ',''))
table['address'] = item.find('div',{"class":"property-address"}).text.replace('n','').replace('  ','') 
table['city'] = item.find('div',{"class":"property-city"}).text.replace('n','').replace('  ','') 
table['beds'] = item.find('div',{"class":"property-beds"}).text.replace('n','').replace('  ','') 
table['baths'] = item.find('div',{"class":"property-baths"}).text.replace('n','').replace('  ','') 
try:
table['half-baths'] = item.find("div",{"class":"property-half-baths"}).text.replace('n','').replace('  ','')
except:
table['half-baths'] = None
try:
table['property sq.ft.'] = item.find("div",{"class":"property-sqft"}).text.replace('  ','').replace("n",'')
except:
table['property sq.ft.'] = None
list1.append(table)
list1

输出

$325,000 
$249,000 
$390,000 
$274,900 
$208,000 
$169,000 
$127,500 
$990,999 

当我打印价格值时,我会得到唯一的值,但当我将所有值附加到列表中时,所有值都会被复制。任何帮助都意义重大。问题:如何摆脱这种数据复制并获得相应的值

[{'address': ' 1129 Hilltop Drive',
'city': 'Rock Springs WY 82901 ',
'beds': '4 beds ',
'baths': '5 baths ',
'half-baths': '2 half baths ',
'property sq.ft.': '10,300 sq. ft '},
{'address': ' 1129 Hilltop Drive',
'city': 'Rock Springs WY 82901 ',
'beds': '4 beds ',
'baths': '5 baths ',
'half-baths': '2 half baths ',
'property sq.ft.': '10,300 sq. ft '},
{'address': ' 1129 Hilltop Drive',
'city': 'Rock Springs WY 82901 ',
'beds': '4 beds ',
'baths': '5 baths ',
'half-baths': '2 half baths ',
'property sq.ft.': '10,300 sq. ft '},
{'address': ' 1129 Hilltop Drive',
'city': 'Rock Springs WY 82901 ',
'beds': '4 beds ',
'baths': '5 baths ',
'half-baths': '2 half baths ',
'property sq.ft.': '10,300 sq. ft '},
{'address': ' 1129 Hilltop Drive',
'city': 'Rock Springs WY 82901 ',
'beds': '4 beds ',
'baths': '5 baths ',
'half-baths': '2 half baths ',
'property sq.ft.': '10,300 sq. ft '},
{'address': ' 1129 Hilltop Drive',
'city': 'Rock Springs WY 82901 ',
'beds': '4 beds ',
'baths': '5 baths ',
'half-baths': '2 half baths ',
'property sq.ft.': '10,300 sq. ft '},
{'address': ' 1129 Hilltop Drive',
'city': 'Rock Springs WY 82901 ',
'beds': '4 beds ',
'baths': '5 baths ',
'half-baths': '2 half baths ',
'property sq.ft.': '10,300 sq. ft '},
{'address': ' 1129 Hilltop Drive',
'city': 'Rock Springs WY 82901 ',
'beds': '4 beds ',
'baths': '5 baths ',
'half-baths': '2 half baths ',
'property sq.ft.': '10,300 sq. ft '}]
for item in all:
table ={} # important
print(item.find('a',{"class":"listing-price"}).text.replace('n','').replace('  ',''))
table['address'] = item.find('div',{"class":"property-address"}).text.replace('n','').replace('  ','') 
table['city'] = item.find('div',{"class":"property-city"}).text.replace('n','').replace('  ','') 
table['beds'] = item.find('div',{"class":"property-beds"}).text.replace('n','').replace('  ','') 
table['baths'] = item.find('div',{"class":"property-baths"}).text.replace('n','').replace('  ','') 
try:
table['half-baths'] = item.find("div",{"class":"property-half-baths"}).text.replace('n','').replace('  ','')
except:
table['half-baths'] = None
try:
table['property sq.ft.'] = item.find("div",{"class":"property-sqft"}).text.replace('  ','').replace("n",'')
except:
table['property sq.ft.'] = None
list1.append(table)

print(set(list1))#循环外打印列表使用集合删除重复

最新更新