js提取指定网站内容(看起来这些数据是通过ajax调用加载的:您应该定位)
优采云 发布时间: 2022-04-11 17:18js提取指定网站内容(看起来这些数据是通过ajax调用加载的:您应该定位)
看起来这些数据是通过 ajax 调用加载的:
您应该定位此 URL:
import requests
import urllib
from bs4 import BeautifulSoup
params = {
"type":"team-detail",
"league":"ncb",
"stat_id":"3083",
"season_id":"312",
"cat_type":"2",
"view":"stats_v1",
"is_previous":"0",
"date":"04/06/2015"
}
content = urllib.request.urlopen("http://www.teamrankings.com/ajax/league/v3/stats_controller.php",data=urllib.parse.urlencode(params).encode('utf8')).read()
soup = BeautifulSoup(content)
table = soup.find("table", attrs={'class':'sortable'})
data = []
rows = table.findAll("tr")
for tr in rows:
cols = tr.findAll("td")
for td in cols:
text = ''.join(td.find(text=True))
data.append(text)
print(data)
使用 Web 检查器,您还可以查看随 POST 请求传递的参数。
通常,另一端的服务器会检查这些值,如果没有或全部丢失,则拒绝您的请求。上面的代码片段对我来说很好。我转向 urllib2 因为我通常更喜欢使用那个库。
如果数据被加载到浏览器中,它可以被抓取。您只需要模仿浏览器发送的请求即可。