0

下記wikiのインフォボックスから位置情報を取得したいと思っています。

これが私が試したことです:

r = requests.get('https://en.wikipedia.org/wiki/Alabama_Department_of_Youth_Services_Schools', proxies = proxies)
html_source = r.text
soup = BeautifulSoup(html_source)

school_d['name'] = soup.find('h1', 'firstHeading').get_text()
print soup.find('th', text=re.compile("location")).find_next_sibling()

出力:None

兄弟ではないため、要素にアクセスできないと思います<td>か??

何かアドバイス?

4

1 に答える 1

1
>>> table = soup.find("table", class_ = "infobox")
>>> name = table.find("th").text
>>> country = table.find("th",text="Country").parent.find("td").text
>>> table = soup.find("table", class_ = "infobox")
>>> name = table.find("th").text
>>> country = table.find("th",text="Country").parent.find("td").text
>>> country = table.find("th",text="Country").find_next_sibling().text #also works
>>> location =  table.find("th",text="Location").parent.find("td").text
>>> location = table.find("th",text="Location").find_next_sibling().text #also works

そんな感じ?

于 2013-08-12T19:03:06.133 に答える