【问题标题】:Grabbing text data from Baseball-reference Python从棒球参考 Python 中获取文本数据
【发布时间】:2015-06-12 22:32:04
【问题描述】:

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

【问题讨论】:

    标签: python web-scraping html-parsing


    【解决方案1】:

    如果您要使用BeautifulSoup 解决它,您将通过文本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'
    

    【讨论】:

    • 这太干净了!做得好。为了完整起见,我们需要一个“import re”,但也许这很明显
    • @JoeSparacino 谢谢,实际上,我们可以在这里使用完全匹配,不需要re,已编辑。
    猜你喜欢
    • 2022-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-27
    • 2015-09-04
    • 1970-01-01
    • 2018-03-17
    • 2020-08-11
    相关资源
    最近更新 更多