如何使用请求库使用参数生成POST请求?

时间:2019-01-18 06:42:52

标签: python http post python-requests

我想将POST请求发送到以下URL->

http://nhb.gov.in/OnlineClient/categorywiseallvarietyreport.aspx?&menu.Menu=11

如果您访问该站点,则存在诸如类别,日期,状态之类的字段。因此,当我填写字段并单击“搜索”按钮并检查“网络”选项卡时,我会收到发送POST请求所需的参数。

因此,我制作了一个包含键,值对的字典,并发送了发布请求。这是我的代码->

import requests

data = {'ctl00$ContentPlaceHolder1$scrpMan':'ctl00$ContentPlaceHolder1$UpdatePanel9013|ctl00$ContentPlaceHolder1$btnSearch' ,
    '__VIEWSTATE':'/wEPDwUKMTMzMzg0OTIwNQ9kFgJmD2QWBAIFDxYCHgVzdHlsZQUtYmFja2dyb3VuZC1pbWFnZTp1cmwoLi4vYXNzZXRzL2ltZy9sb2dvLnBuZyk7ZAIHD2QWAgIBD2QWAgIBD2QWAmYPZBYKAgcPEA8WBh4NRGF0YVRleHRGaWVsZAUIQ2F0Z05hbWUeDkRhdGFWYWx1ZUZpZWxkBQhjYXRnY29kZR4LXyFEYXRhQm91bmRnZBAVBA9TZWxlY3QgQ2F0ZWdvcnkHRkxPV0VSUwZGUlVJVFMKVkVHRVRBQkxFUxUEATABNAExATIUKwMEZ2dnZ2RkAg0PEA8WBh8BBQpDZW50cmVOYW1lHwIFDENlbnRyZU51bWJlch8DZ2QQFR4JQUhNRURBQkFECEFNUklUU0FSBkJBUkFVVAlCZW5nYWx1cnUGQkhPUEFMDEJIVUJBTkVTSFdBUgpDSEFORElHQVJIB0NIRU5OQUkIREVIUkFEVU4FREVMSEkIR0FO…FZBUkFOQVNJFR4BMQIzMgIzNgEyATMBNAE3AjE1AjM0ATgBOQIxMAIxMQIxMgIyMgE2AjM5AjE0ATUCMTYCMjMCMTcCNDACMjQCMzUCMjgCMTgCMTkCMjACNDEUKwMeZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZGQCFw8PZBYCHgdvbmNsaWNrBTVzZWxlY3RBbGxDZW50ZXIoJ1N5c3RlbS5XZWIuVUkuV2ViQ29udHJvbHMuTGlzdEJveCcpO2QCGQ8PZBYCHwQFNVJlbW92ZUFsbENlbnRlcignU3lzdGVtLldlYi5VSS5XZWJDb250cm9scy5MaXN0Qm94Jyk7ZAIjDw8WAh4HVmlzaWJsZWhkZBgBBR5fX0NvbnRyb2xzUmVxdWlyZVBvc3RCYWNrS2V5X18WAQUpY3RsMDAkQ29udGVudFBsYWNlSG9sZGVyMSRMc2JveENlbnRlckxpc3QVB5GC6UfPXNBmxBLvA2devnEGKA==',
    '__VIEWSTATEGENERATOR':'DBC76A8E',
    'ctl00$ContentPlaceHolder1$drpCategoryName':'4',
    'ctl00$ContentPlaceHolder1$LsboxCenterList':'1',
    'ctl00$ContentPlaceHolder1$txtdate':'10/01/2019',
    '__ASYNCPOST':'true',
    'ctl00$ContentPlaceHolder1$btnSearch':'Search'}

r = requests.post(url = 'http://nhb.gov.in/OnlineClient/categorywiseallvarietyreport.aspx?&menu.Menu=11', data = data)

print(r.text)

运行脚本时,出现错误提示

  

179 |错误| 500 |页面正在执行异步回发,但是ScriptManager.SupportsPartialRendering属性设置为false。确保在异步回发期间将该属性设置为true。|

如果我尝试删除一些参数,则会收到一个响应,该响应仅给出页面源。

我想收到所有州和任何日期的任何类别价格的回复。 (在我填写字段并点击搜索按钮后,会显示一个表格。)

这是我在做错什么吗?

1 个答案:

答案 0 :(得分:1)

您需要添加标题才能使请求生效。我对其进行了测试,并足以仅添加User-Agent:

import requests

headers = {
    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:64.0) Gecko/20100101 Firefox/64.0'
}

data = {
  'ctl00$ContentPlaceHolder1$scrpMan': 'ctl00$ContentPlaceHolder1$UpdatePanel9013|ctl00$ContentPlaceHolder1$btnSearch',
  '__EVENTTARGET': '',
  '__EVENTARGUMENT': '',
  '__VIEWSTATE': '/wEPDwUKMTMzMzg0OTIwNQ9kFgJmD2QWBAIFDxYCHgVz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ZBgBBR5fX0NvbnRyb2xzUmVxdWlyZVBvc3RCYWNrS2V5X18WAQUpY3RsMDAkQ29udGVudFBsYWNlSG9sZGVyMSRMc2JveENlbnRlckxpc3QVB5GC6UfPXNBmxBLvA2devnEGKA==',
  '__VIEWSTATEGENERATOR': 'DBC76A8E',
  'ctl00$ContentPlaceHolder1$drpCategoryName': '4',
  'ctl00$ContentPlaceHolder1$LsboxCenterList': '1',
  'ctl00$ContentPlaceHolder1$txtdate': '10/01/2019',
  '__ASYNCPOST': 'true',
  'ctl00$ContentPlaceHolder1$btnSearch': 'Search'
}

response = requests.post('http://nhb.gov.in/OnlineClient/categorywiseallvarietyreport.aspx?&menu.Menu=11', data=data, headers=headers)
print(response.text)