从网站中提取 table

Extracting a table from a website

我曾多次尝试在此网站上检索 table: http://www.whoscored.com/Players/845/History/Tomas-Rosicky ("Historical Participations"下的那个)

import urllib2 
from bs4 import BeautifulSoup 
soup = BeautifulSoup(urllib2.urlopen('http://www.whoscored.com/Players/845/').read())

这是我用来检索 table html 的 Python 代码,但我得到的是一个空字符串。帮帮我!

所需的 table 是通过对 http://www.whoscored.com/StatisticsFeed/1/GetPlayerStatistics 端点请求的 异步 API 调用 形成的,return 是一个JSON 响应。换句话说,urllib2 会 return 给你一个没有 "dynamic" 部分的初始 HTML 页面内容。换句话说,urllib2 不是浏览器。

您可以使用浏览器开发者工具研究请求:

现在,您需要在代码中模拟此请求。 requests package 是您应该考虑使用的东西。

这是一个关于 whoscored.com 的类似问题,我之前已经回答过,这里有一个示例工作代码,您可以将其用作起点:

  • XHR request URL says does not exist when attempting to parse it's content