不必要的重复美丽的环状循环

时间:2016-09-23 14:06:16

标签: python web-scraping beautifulsoup nested-loops

使用以下代码,我能够提取位置类型(医院,紧急护理等),位置名称及其地址。然而,我提出了一堆重复。如何摆脱重复,只保留每个位置的一条记录?

from bs4 import BeautifulSoup
import csv

file = open("/home/daniel/Downloads/wellstar.html")
soup = BeautifulSoup(file,"html.parser")

types = soup.findAll("h3",class_="WebFont SpotBodyGreen")
names = soup.findAll("div",class_="PurpleBackgroundHeading")
addresses = soup.findAll("div",class_="WS_Location")

locations_saved=""
for type in types:
   locations=""
    for name in names:
        for address in addresses:
            locations=locations+"\n"+type.text+name.text+address.text
locations_saved=locations_saved+"\n"+locations[1:]
print(locations_saved)

0 个答案:

没有答案