繁体   English   中英

卷曲vs请求python 3

[英]Curl vs requests python 3

我正在对服务发出卷曲请求:

curl -v --data "cp4=2765&cp3=350&method%3AsearchPC2=Procurar" https://www.ctt.pt/feapl_2/app/open/postalCodeSearch/postalCodeSearch.jspx

我可以看到它是成功的,因为我们在响应正文中有一个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>
...

有线的问题是,如果我使用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

但是失败了,服务器没有向我发送结果html块

如果您在环境中配置了代理,请在会话/请求中也定义它。

例如会话:

    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)  

请参阅文档:
请求http://docs.python-requests.org/en/master/user/quickstart/
会话http://docs.python-requests.org/en/master/user/advanced/

另一个选择可以是安全性,如果您使用的ssl有问题,请添加verify = False例如:

    response = requests.get('http://my.domain.com', verify=False)

当我尝试以下内容时,我正在输出。

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

尝试根据需要解析内容。 我正在获取输出。 我试图更改headers = {'Content-Type': 'application/x-www-form-urlencoded'}仍然可以得到一些输出。

注意:我正在使用python2.7

当服务器正在搜索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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM