【问题标题】:BeautifulSoup not returning sourceBeautifulSoup 不返回源
【发布时间】:2013-05-30 06:41:51
【问题描述】:

我正在尝试从http://www.footywire.com/afl/footy/ft_match_statistics?mid=5634 下载表格数据 但是当我尝试从 BeautifulSoup 获取汤时遇到问题

我在努力

url='http://www.footywire.com/afl/footy/ft_match_statistics?mid=5634'

soup=BeautifulSoup(url)

但只是取回标题,或者什么都没有。

我也尝试过使用不同的解析器 (html5lib),并通过 urllib2 读取页面,但仍然没有得到页面的任何正文。我在网络交互方面毫无用处,所以也许我缺少一些基本的东西,但它似乎在其他网站上也能工作。

在提取这些数据时,我们将不胜感激。为什么我没有得到预期的来源?

【问题讨论】:

    标签: web-scraping beautifulsoup urllib2


    【解决方案1】:

    你好,澳大利亚人:)

    如果我是你,我会使用 requests 和 lxml。我认为该网站正在检查 cookie 和一些标题。 Requests 的 session 类存储 cookie,也可以让你传递 headers。 lxml 会让你在这里使用 xpath,我认为这会比 BeautifulSoup 的界面更痛苦。

    见下文:

    >>> import lxml.html
    >>> import requests
    >>> session = requests.session()
    >>> response = session.get("http://www.footywire.com/afl/footy/ft_match_statistics?mid=5634", headers={"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36","Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Referer":"http://www.footywire.com/afl/footy/ft_match_statistics?mid=5634","Cache-Control":"max-age=0"})
    >>> tree = lxml.html.fromstring(response.text)
    >>> rows = tree.xpath("//table//table//table//table//table//table//tr")
    >>> for row in rows:
    ...     row.xpath(".//td//text()")
    ... 
    [u'\xa0\xa0', 'Sydney Match Statistics (Sorted by Disposals)', 'Coach: ', 'John Longmire', u'\xa0\xa0']
    ['Player', 'K', 'HB', 'D', 'M', 'G', 'B', 'T', 'HO', 'I50', 'FF', 'FA', 'DT', 'SC']
    ['Josh Kennedy', '20', '17', '37', '2', '1', '1', '1', '0', '3', '1', '0', '112', '126']
    ['Jarrad McVeigh', '23', '11', '34', '1', '0', '0', '2', '0', '5', '1', '1', '100', '116']
    ... cont...
    

    xpath 查询可能有点脆弱,但你明白了 :)

    【讨论】:

      猜你喜欢
      • 2015-01-02
      • 1970-01-01
      • 1970-01-01
      • 2022-11-19
      • 2021-11-21
      • 2020-10-13
      • 2015-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多