发布请求返回空字符串(python)

时间:2017-02-21 21:00:27

标签: python-3.x http-post

我需要从网站获取数据。要获得此数据,用户必须先输入邮政编码。在探索源代码后,我得到了以下内容。

响应结果(毕竟我需要的是

{PostCodePK: 16666, PostCode: "7468", City: "MACQUARIE HEADS", State: "TAS", Country: "AUST",…}
1
:
{PostCodePK: 16667, PostCode: "7468", City: "STRAHAN", State: "TAS", Country: "AUST",…}

请求数据。

Request URL:http://www.lucasmill.com/Resources/ws-common.aspx
Request Method:POST
Status Code:200 OK
Remote Address:111.67.1.113:80
Response Headers
view source
Cache-Control:private
Content-Length:321
Content-Type:application/json; charset=utf-8
Date:Tue, 21 Feb 2017 20:19:26 GMT
Expires:Tue, 21 Feb 2017 20:19:26 GMT
Server:Microsoft-IIS/8.5
Set-Cookie:dnn_IsMobile=False; path=/; HttpOnly
Request Headers
view source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8,ru;q=0.6,uk;q=0.4
Connection:keep-alive
Content-Length:18
Content-Type:application/json; charset=UTF-8
Cookie:.ASPXANONYMOUS=ptTlH_jC0gEkAAAAZTU4MTA5NTItZmNlZS00MzRjLThmYTgtMWZkYWNkOTEwZmY00; dnn_IsMobile=False; language=en-AU; __utmt=1; __utma=97280258.254723646.1487697408.1487697408.1487708346.2; __utmb=97280258.1.10.1487708346; __utmc=97280258; __utmz=97280258.1487697408.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
DNN-Service:true
DNN-Service-Method:GetTown
DNT:1
Host:www.lucasmill.com
Origin:http://www.lucasmill.com
Referer:http://www.lucasmill.com/Sawmilling-Contractors
User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
X-Requested-With:XMLHttpRequest
Request Payload
{ 'param':'7468' }

这是我的python代码。

import requests
r = requests.post('http://www.lucasmill.com/Resources/ws-common.aspx',data={      'param':'7468' })
print(r.text)

但我收到的所有内容都是一个空字符串。 我哪里错了?

1 个答案:

答案 0 :(得分:0)

您需要添加一些特定应用程序所需的额外标头。 如果您在浏览器中查看请求标头转储,则可以看到以下内容:

enter image description here

所以,翻译成python它看起来像:

import json
import requests

headers = {
        'DNN-Service': 'true',
        'DNN-Service-Method': 'GetTown',
}

r = requests.post('http://www.lucasmill.com/Resources/ws-common.aspx',data=json.dumps({'param':7468}), headers=headers)
print(r.text)

<强>输出

  

[none] [22:28:30] vlazarenko @ alluminium(〜/ tests)$ python post.py   [{&#34; PostCodePK&#34;:16666&#34;邮编&#34;:&#34; 7468&#34;&#34;市&#34;:&#34; MACQUARIE   HEADS&#34;&#34;国&#34;:&#34; TAS&#34;&#34;国家&#34;:&#34; AUST&#34;&#34;纬度&#34 ;: &#34; -42.2149353&#34;&#34;经度&#34;:&#34; 145.1951436&#34;&#34; CanGeocode&#34;:真},{&#34; PostCodePK&#34 ;:16667,&#34;邮编&#34;:&#34; 7468&#34;&#34;市&#34;:&#34;斯特拉汉&#34;&#34;国&#34;:& #34; TAS&#34;&#34;国家&#34;:&#34; AUST&#34;&#34;纬度&#34;:&#34; -42.1534771&#34;&#34;经度&#34;:&#34; 145.3281242&#34;&#34; CanGeocode&#34;:真}]