我想對以前提出的問題展開:循環多個值到字典
Nested For Loop with Unequal Entities
在這個問題,我要求的方法來提取位置的類型(醫院,緊急護理等)在除了地點的名稱(WELLSTAR亞特蘭大醫療中心,WELLSTAR亞特蘭大南部醫療中心等)。
答案建議使用for循環和字典來收集值和鍵。代碼片段如下所示:
from pprint import pprint
import requests
from bs4 import BeautifulSoup
url = "https://www.wellstar.org/locations/pages/default.aspx"
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")
d = {}
for row in soup.select(".WS_Content > .WS_LeftContent > table > tr"):
title = row.h3.get_text(strip=True)
d[title] = [item.get_text(strip=True) for item in row.select(".PurpleBackgroundHeading a)]
pprint(d)
我想擴展現有解決方案以包含與適當的鍵值組合匹配的實體地址。如果最好的解決方案是利用字典之外的其他東西,我也會接受這個建議。
雖然涉及到前一個問題,[不要讓這一個適當的問題以及](http://stackoverflow.com/help/how-to-ask)。 –