从响应标头获取位置

时间:2016-08-04 22:13:48

标签: python python-requests

我正在尝试使用python的请求模块从Location请求中获取POST值。但是,当我查看响应的标题时,我看不到任何这样的键。使用谷歌浏览器执行相同的请求确实显示了密钥。

这是我尝试从以下位置下载数据的地方:https://data.police.uk/data 。在Google Chrome中启动此功能并打开开发人员工具。当您选择日期范围时,选择一些强制并点击Generate File,您可以在响应标题中看到POST项的Location请求。

import requests
from urlparse import urlparse, urljoin

BASE = 'https://data.police.uk'
FORM_PATH = 'data'

form_url = urljoin(BASE, FORM_PATH)

# Get data download URL
client = requests.session()
try:
    client.get(form_url)
except requests.exceptions.ConnectionError as e:
    print (e)
    sys.exit()

csrftoken = client.cookies.values()

l = [('forces', 'cleveland')]
t = ('csrfmiddlewaretoken', csrftoken[0])

d_from = ('date_from', '2014-05')
d_to = ('date_to', '2016-05')
l.extend((t, d_from, d_to))

r = client.post(form_url, headers=dict(Referer=form_url), data=l)

查询响应标题给了我:

In [4]: r.headers
Out[4]: {'Content-Length': '4332', 'Content-Language': 'en-gb', 'Content-Encoding': 'gzip', 'Set-Cookie': 'csrftoken=aGQ7kO4tQ2cPD0Fp2svxxYBRe4rAk0kw; expires=Thu, 03-Aug-2017 22:11:44 GMT; Max-Age=31449600; Path=/', 'Vary': 'Cookie, Accept-Language', 'Server': 'nginx', 'Connection': 'keep-alive', 'Date': 'Thu, 04 Aug 2016 22:11:44 GMT', 'Content-Type': 'text/html; charset=utf-8'}

问题:如何从响应标头中获取Location密钥?

修改

答案:必须指定l.append(['include_crime', 'on'])。在此之后工作。

1 个答案:

答案 0 :(得分:-1)

EDIT2

您也需要传递include_crime = on语句,因为您没有选择任何数据集。在网页上,如果您没有选中任何复选框,您将获得相同的页面,并且您将无法获得任何位置标题。这就是为什么你的r.content有"请选择至少一个数据集"。

相关问题