habe das folgende Problem:
Beispiel:
Code: Alles auswählen
g_data = soup.find_all("div", {"class": "itemsContent clearafter"})
for item in g_data:
header = item.find_all("div", {"class": "InnprodInfos"})
print(header[0].contents[0].text.strip())
Code: Alles auswählen
DMZ 3rd Tunnel - Korean Demilitarized Zone Day Tour from Seoul
Panmunjeom Day Tour
Seoul City Half Day Private Tour
The Soul of Seoul - Small Group Tour
Seoul Helicopter Tour
Seoul City Full Day Tour
Seoul City Half Day Tour
The Street Museum in the Urban Core - Small Group Tour
Korean Folk Village Day Tour
DMZ 3rd Tunnel - Korean Demilitarized Zone Day Tour from Seoul
Panmunjeom Day Tour
Seoul City Half Day Private Tour
The Soul of Seoul - Small Group Tour
Seoul Helicopter Tour
Seoul City Full Day Tour
Seoul City Half Day Tour
The Street Museum in the Urban Core - Small Group Tour
Korean Folk Village Day Tour
Wie ihr sehen könnt, bekommen ich den gewünschten Ouput doppelt wieder. Idealerweise würde ich den nur einmal bekommen.
Daher habe ich versucht, die Duplikate zu entfernen. Zuerst mit list und dann mit set
list:
Code: Alles auswählen
result = []
for item in g_data:
header = item.find_all("div", {"class": "InnprodInfos"})
item = header[0].contents[0].text.strip()
if item not in result:
result.append(item)
print( '\n'.join(result))
Set:
Code: Alles auswählen
result = set()
for item in g_data:
header = item.find_all("div", {"class": "InnprodInfos"})
result.add(header[0].contents[0].text.strip())
print('\n'.join(result))
Habe es weiter versucht:
Code: Alles auswählen
already_printed = set()
for item in g_data:
header = item.find_all("div", {"class": "InnprodInfos"})
item = header[0].contents[0].text.strip()
if item not in already_printed:
print(item)
already_printed.add(item)