【问题标题】:Python Requests API requests.get() call with params is not working as intended (response ignores params)使用参数的 Python 请求 API requests.get() 调用未按预期工作(响应忽略参数)
【发布时间】:2017-09-29 23:54:23
【问题描述】:

这是我的代码:

import requests

find_doctors_url = 'http://www.americhoice.com/find_doctor/Ver2/results_doc.jsp'

payload ={"specialty":"ORSU","docproducts":"HOBD,HLOP","planNameDropDoc":"HOBD,HLOP","plan":"uhcwa","zip":"98122","zipradius":"10","findButton":"FIND DOCTOR","specialtyName":"ORTHOPAEDIC SURGERY"}

response = requests.get(find_doctors_url,params=payload)
print(response.url)
print(response.content)

当我打印 response.content 时,我收到的只是:

<!-- NEAADR0179 -Anil Kumar Vutikuri *** End-->


<!--BEGIN SETTING HEADERS TO NO CACHE-->

<!--END SETTING HEADERS TO NO CACHE-->

<!--SET SESSION VALUES FROM URL PARAMETERS-->


<!--END SET SESSION VALUES FROM URL PARAMETERS-->

当您导航到: 查看源代码:http://www.americhoice.com/find_doctor/Ver2/results_doc.jsp

但是,当您导航到由 response.url 生成的 url 时,我正在寻求返回收到的完整 html

查看源代码:http://www.americhoice.com/find_doctor/Ver2/results_doc.jsp?specialty=ORSU&docproducts=HOBD%2CHLOP&planNameDropDoc=HOBD%2CHLOP&plan=uhcwa&zip=98122&zipradius=10&findButton=FIND+DOCTOR&specialtyName=ORTHOPAEDIC+SURGERY

问题似乎是请求没有正确发送 GET 查询参数

我尝试过的事情(不成功): 1)请求完整的 URL(编码)而不是使用 params 字典 2)使用 urllib3 库而不是 Requests 库

【问题讨论】:

    标签: python web-scraping get python-requests urllib3


    【解决方案1】:

    首先,你给的查看源网站返回404错误。

    第二,打印response.url会得到什么?它应该返回完整的 url,包括 params 连接。

    【讨论】:

    • 啊,之前我在同一个会话中它会出现,现在当我重新启动浏览器时,我得到一个空白屏幕(仍然是 200 个请求)。必须需要一些会话数据
    • @AmiyaDiwan 所以在发送GET 请求之前,您可能需要登录或其他什么?在这种情况下,您应该使用requests.session 在不同的请求期间保持会话。
    • 感谢@Sraw,这就是修复!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-17
    • 1970-01-01
    • 2018-10-19
    • 2016-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多