【问题标题】:Incorrect response from post request with requests带有请求的发布请求的响应不正确
【发布时间】:2017-03-16 08:34:26
【问题描述】:

搜索网址 - http://aptaapps.apta.org/findapt/Default.aspx?UniqueKey=.

需要获取邮政编码的数据(10017) 发送帖子请求,但我收到搜索页面(来自搜索 url 的响应),但没有收到结果页面。

我的代码:

# -*- coding: UTF-8 -*-

import requests
from bs4 import BeautifulSoup, element


search_url = "http://aptaapps.apta.org/findapt/Default.aspx?UniqueKey="
session = requests.Session()
r = session.get(search_url)
post_page = BeautifulSoup(r.text, "lxml")
try:
    target_value = post_page.find("input", id="__EVENTTARGET")["value"]
except TypeError:
    target_value = ""

try:
    arg_value = post_page.find("input", id="__EVENTARGUMENT")["value"]
except TypeError:
    arg_value = ""

try:
    state_value = post_page.find("input", id="__VIEWSTATE")["value"]
except TypeError:
    state_value = ""

try:
    generator_value = post_page.find("input", id="__VIEWSTATEGENERATOR")["value"]
except TypeError:
    generator_value = ""

try:
    validation_value = post_page.find("input", id="__EVENTVALIDATION")["value"]
except TypeError:
    validation_value = ""

post_data = {
            "__EVENTTARGET": target_value,
            "__EVENTARGUMENT": arg_value,
            "__VIEWSTATE": state_value,
            "__VIEWSTATEGENERATOR": generator_value,
            "__EVENTVALIDATION": validation_value,
            "ctl00$SearchTerms2": "",
            "ctl00$maincontent$txtZIP": "10017",
            "ctl00$maincontent$txtCity": "",
            "ctl00$maincontent$lstStateProvince": "",
            "ctl00$maincontent$radDist": "1",
            "ctl00$maincontent$btnSearch": "Find a Physical Therapist"
            }

headers = {
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
        "Accept-Encoding": "gzip, deflate",
        "Accept-Language": "ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4",
        "Cache-Control": "max-age=0",
        "Content-Length": "3025",
        "Content-Type": "application/x-www-form-urlencoded",
        "Host": "aptaapps.apta.org",
        "Origin": "http://aptaapps.apta.org",
        "Proxy-Connection": "keep-alive",
        "Referer": "http://aptaapps.apta.org/findapt/default.aspx?UniqueKey=",
        "Upgrade-Insecure-Requests": "1",
        "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"
        }

post_r = session.post(search_url, data=post_data, headers=headers)
print(post_r.text)

【问题讨论】:

    标签: python python-3.x post beautifulsoup python-requests


    【解决方案1】:

    简答:

    尝试替换:

    post_r = session.post(search_url, data=post_data, headers=headers)
    

    到:

    post_r = session.post(search_url, json=post_data, headers=headers)
    

    长答案:

    对于 POST 方法,有多种数据类型可以发布,如form-datax-www-form-urlencodedapplication/jsonfile 等。

    您应该知道发布数据的类型。有一个很棒的 chrome 插件叫做postman。您可以使用它来尝试不同的数据类型并找出正确的数据类型。

    找到后,在requests.post中使用正确的参数key,如果是form-datax-www-form-urlencoded,则参数data。参数json 为json格式。可以参考requests文档了解更多参数。

    【讨论】:

    • 您好 Kingname,感谢您的回答。数据类型是“x-www-form-urlencoded”,所以参数正确但代码不起作用。 P.S.:我尝试将其更改为 json。没用。
    • OK, 1. 从你的标题中删除"Content-Length": "3025"。并重试。 2.如果第1步还是不行,请安装postman,试试看能否发出正确的请求。
    • 1.不工作。 2.我已经安装了,但是还是不行。
    • @GiveItAwayNow 如果是这样,则意味着您发布的网址或标题或数据不正确。这不关python的事。请研究网站,使用 fiddler 或 charles 捕获数据包以找到解决方案。
    • @GiveItAwayNow 总而言之,只有在邮递员中工作,才能在python中解决问题。
    猜你喜欢
    • 2021-11-04
    • 2017-08-31
    • 2018-12-11
    • 1970-01-01
    • 2014-07-06
    • 2021-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多