[英]Curl vs requests python 3
I am doing a curl post request to a service: 我正在对服务发出卷曲请求:
curl -v --data "cp4=2765&cp3=350&method%3AsearchPC2=Procurar" https://www.ctt.pt/feapl_2/app/open/postalCodeSearch/postalCodeSearch.jspx
I can see that it successful because we have a div with the results in the response body: 我可以看到它是成功的,因为我们在响应正文中有一个div,其中包含结果:
...
<div class="highlighted-result text-left">
<h4 class="subheader">Rua Sacadura Cabral</h4>
<h4 class="subheader">Ímpares de 11 a 233</h4>
<h3 class="subheader">Galiza</h3>
<h2>2765-350 ESTORIL</h2>
</div>
...
The wired thing is that if I do it with python + requests, it doesn't give me the expected result as the curl above does, I even tried to set the user agent to the same as curl: 有线的问题是,如果我使用python +请求执行此操作,则不会像上面的curl那样给我预期的结果,我什至尝试将用户代理设置为与curl相同:
import requests as r
headers_p = {
'User-Agent': 'curl/7.47.0',
'Host': 'www.ctt.pt'
}
payload = {'cp4': 2765, 'cp3': 350, 'method':'', 'searchPC2': 'Procurar'}
req_p = r.post('https://www.ctt.pt/feapl_2/app/open/postalCodeSearch/postalCodeSearch.jspx', data=payload)
print(req_p.text) # doesn't have the the same content as the curl, I need the html block above
But it fails, the server doesn't send me the results html block 但是失败了,服务器没有向我发送结果html块
In case you have a proxy configured at your environment, define it at your session/request as well. 如果您在环境中配置了代理,请在会话/请求中也定义它。
For example with session: 例如会话:
my_proxies = {
'http': 'http://myproxy:8080',
'https': 'https://myproxy:8080'
}
session = requests.Session()
request = requests.Request('POST', 'http://my.domain.com', data=params_template, headers=req_headers, proxies=my_proxies)
prepped = session.prepare_request(request)
response = session.send(prepped)
see documentation: 请参阅文档:
request http://docs.python-requests.org/en/master/user/quickstart/ 请求http://docs.python-requests.org/en/master/user/quickstart/
session http://docs.python-requests.org/en/master/user/advanced/ 会话http://docs.python-requests.org/en/master/user/advanced/
Another option can be the security, in case you use have a problem with the ssl, add verify=False For example: 另一个选择可以是安全性,如果您使用的ssl有问题,请添加verify = False例如:
response = requests.get('http://my.domain.com', verify=False)
When I try the following i am getting output. 当我尝试以下内容时,我正在输出。
import requests as r
from requests import Response
headers = {'Content-Type': 'application/xml'}
payload = {'cp4': 2765, 'cp3': 350, 'method':'', 'searchPC2': 'Procurar'}
given_url = 'https://www.ctt.pt/feapl_2/app/open/postalCodeSearch/postalCodeSearch.jspx'
req_p = r.post(given_url, data=payload, headers=headers)
print req_p, req_p.text
Try to parse the content as you need. 尝试根据需要解析内容。 I am getting the output. 我正在获取输出。 I tried to change the headers = {'Content-Type': 'application/x-www-form-urlencoded'}
still i am getting some output. 我试图更改headers = {'Content-Type': 'application/x-www-form-urlencoded'}
仍然可以得到一些输出。
Note: I am using python2.7 注意:我正在使用python2.7
Please use the below code as the server is searching for json format. 当服务器正在搜索json格式时,请使用以下代码。
import requests as r
import json
from requests import Response
headers = {'Content-Type': 'application/json'}
payload = {'cp4': 2765, 'cp3': 350, 'method':'', 'searchPC2': 'Procurar'}
data_json = json.dump(payload)
given_url = 'https://www.ctt.pt/feapl_2/app/open/postalCodeSearch/postalCodeSearch.jspx'
req_p = r.post(given_url, data=data_json, headers=headers)
print req_p, req_p.text
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.