"响应对象没有属性' body'"

时间:2014-09-03 22:06:55

标签: python xmlhttprequest

我正在使用以下代码在网页上模仿XHR请求,以便在屏幕上选择不同的标签按钮时更新表格对象的内容:

import requests

url = 'http://www.whoscored.com/stageplayerstatfeed/?field=1&isAscending=false&orderBy=Rating&playerId=-1&stageId=9155&teamId=32"'

params = {'d': date.strftime('%Y%m'), 'isAggregate': 'false'}
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36'}

response = requests.get(url, params=params, headers=headers)

fixtures = response.body
#fixtures = literal_eval(response.content)
print fixtures 

这会在帖子的标题中抛出错误。我猜我需要添加参数' body'不知何故在requests.get()声明中,但我不确定究竟是多少。有人可以帮忙吗?

由于

1 个答案:

答案 0 :(得分:3)

您要查找的属性是content

>>> import requests
>>> response = requests.get('http://google.com')
>>> response.content
'<!doctype html><html itemscope=""...

实际上,您的代码看起来像这样:

import requests

url = 'http://www.whoscored.com/stageplayerstatfeed/?field=1&isAscending=false&orderBy=Rating&playerId=-1&stageId=9155&teamId=32"'

params = {'d': date.strftime('%Y%m'), 'isAggregate': 'false'}
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36'}

response = requests.get(url, params=params, headers=headers)

fixtures = response.content
#fixtures = literal_eval(response.content)
print fixtures