您需要一種按名稱對位置進行分組的方法。對於這一點,我們每個塊分開,讓收集到一本字典的名稱和地點:
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)
打印(適合打印用pprint()
):
{'WellStar Community Hospice': ['Tranquility at Cobb Hospital',
'Tranquility at Kennesaw Mountain'],
'WellStar Health Parks': ['Acworth Health Park', 'East Cobb Health Park'],
'WellStar Hospitals': ['WellStar Atlanta Medical Center',
'WellStar Atlanta Medical Center South',
'WellStar Cobb Hospital',
'WellStar Douglas Hospital',
'WellStar Kennestone Hospital',
'WellStar North Fulton Hospital',
'WellStar Paulding Hospital',
'WellStar Spalding Regional Hospital',
'WellStar Sylvan Grove Hospital',
'WellStar West Georgia Medical Center',
'WellStar Windy Hill Hospital'],
'WellStar Urgent Care Centers': ['WellStar Urgent Care in Acworth',
'WellStar Urgent Care in Kennesaw',
'WellStar Urgent Care in Marietta - Delk '
'Road',
'WellStar Urgent Care in Marietta - East '
'Cobb',
'WellStar Urgent Care in Marietta - '
'Kennestone',
'WellStar Urgent Care in Marietta - Sandy '
'Plains Road',
'WellStar Urgent Care in Smyrna',
'WellStar Urgent Care in Woodstock']}
你能解釋一下什麼是在'd執行[title] = [item.get_text(strip = True)for row.select(「。PurpleBackgroundHeading a」)]'line?我懷疑你是在哪裏加入字典的標題密鑰的價值?如果是這樣,我將如何去爲每個鍵添加另一個值。例如,我將如何將每個位置的地址添加到字典中? – Daniel
@丹尼爾好吧,如果您需要進一步的幫助,請將其制定爲單獨的問題!謝謝。 – alecxe