Curl与请求python 3

2024-10-01 19:16:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在向一个服务发出卷发请求:

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

我可以看到它是成功的,因为我们有一个div,结果在响应体中:

^{pr2}$

有线的情况是,如果我用python+请求进行处理,它不会像上面的curl那样给出预期的结果,我甚至尝试将user agent设置为与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块


Tags: thehttpsptappdatawwwopencurl
3条回答

请使用以下代码,因为服务器正在搜索json格式。在

^{1}$

如果您在您的环境中配置了一个代理,那么也可以在您的会话/请求中定义它。在

例如会话:

^{1}$

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

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

^{pr2}$

当我尝试以下操作时,我得到了输出。在

^{1}$

根据需要尝试分析内容。我得到输出。 我试图更改headers = {'Content-Type': 'application/x-www-form-urlencoded'},但仍有一些输出。在

注意:我使用的是python2.7

相关问题 更多 >

    热门问题