使用BeautifulSoup检索位置数据

时间:2013-08-12 18:49:26

标签: python beautifulsoup

我想从以下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>元素,因为它不是兄弟姐妹?

有什么建议吗?

1 个答案:

答案 0 :(得分: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

类似的东西?