从Baseballreference Python获取文本数据

2024-10-01 22:32:33 发布

您现在位置:Python中文网/ 问答频道 /正文

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:之后得到所有东西。在


Tags: 数据文本rightcomidhttpwwwsplit
1条回答
网友
1楼 · 发布于 2024-10-01 22:32:33

如果要用^{}解决它,您将通过文本Throws:找到b标记,并得到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'

相关问题 更多 >

    热门问题