从Baseball-reference Python中获取文本数据

时间:2015-06-12 22:32:04

标签: python web-scraping html-parsing

http://www.baseball-reference.com/players/split.cgi?id=aardsda01&year=2015&t=p

我想得到这位投手投球的数据。如果它是一个表格,我将能够获取数据,但我不知道如何获取文本。

David Aardsma    \ARDS-mah\

David Allan Aardsma (twitter: @TheDA53)

Position: Pitcher
Bats: Right, Throws: Right 
Height: 6' 3", Weight: 220 lb.

文字看起来像这样。我想在Throws:之后得到所有内容。

1 个答案:

答案 0 :(得分:2)

如果您要使用BeautifulSoup解决问题,则可以按文字b找到Throws:标记并获取following sibling

>>> from urllib2 import urlopen
>>> from bs4 import BeautifulSoup
>>>
>>> url = "http://www.baseball-reference.com/players/split.cgi?id=aardsda01&year=2015&t=p"
>>> soup = BeautifulSoup(urlopen(url))
>>> soup.find("b", text='Throws:').next_sibling.strip()
u'Right'