简体   繁体   English

如何使用Python请求处理cgi表单

[英]How to handle cgi form with Python requests

I'm trying to use the requests module in Python to handle a cgi and can't work out what I've done wrong. 我正在尝试使用Python中的请求模块来处理cgi而无法弄清楚我做错了什么。

I've tried to use Google Dev Tools in Chrome to provide the right params and data but I've not quite fixed it. 我曾尝试在Chrome中使用Google Dev Tools来提供正确的参数和数据,但我还没有完全修复它。

The site I'm trying to get data from is: http://staffordshirebmd.org.uk/cgi/birthind.cgi 我试图从中获取数据的网站是: http//staffordshirebmd.org.uk/cgi/birthind.cgi

Here's my code 这是我的代码

import requests 

headers = {"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
        "Accept-Encoding":"gzip,deflate,sdch",
        "Accept-Language":"en-US,en;q=0.8",
        "Cache-Control":"no-cache",
        "Connection":"keep-alive",
        "Content-Length":"124",
        "Content-Type":"application/x-www-form-urlencoded",
        "DNT":"1",
        "Host":"staffordshirebmd.org.uk",
        "Origin":"http://staffordshirebmd.org.uk",
        "Pragma":"no-cache",
        "Referer":"http://staffordshirebmd.org.uk/cgi/birthind.cgi?county=staffordshire",
        "User-Agent":"Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25"}

payload = {"county":"staffordshire",
          "lang": "",
          "year_date":"1837",
          "search_region":"All",
          "sort_by":"alpha",
          "csv_or_list":"screen",
          "letter":"A",
          "submit":"Display Indexes"}

f = requests.put(path, data=payload, headers=headers)

f.text

This provides the response: 这提供了响应:

u'<html>\n<body>\n<div>\n<p>\nThe Bookmark you have used to reach this page is not valid.\n</p>\n<p>\nPlease click <a href="http://staffordshirebmd.org.uk/">here</a> to return to the main page and reset your\nbookmark to that page.\n</p>\n</div>\n</body>\n</html>\n\n'

What am I doing wrong? 我究竟做错了什么?

The URL you used in your Referrer header has a form that uses POST , not PUT . 您在Referrer标头中使用的URL具有使用POST而非PUT的表单。

You rarely should hardcode the Content-Length header; 你很少应该对Content-Length标题进行硬编码; do leave that up to requests to calculate for you and set. 请留下requests为您计算和设置。 Different browsers can easily use subtly different content lengths, a script that only works with a fixed Content-Length would not last long. 不同的浏览器可以轻松地使用微妙的不同内容长度,只能使用固定Content-Length长度的脚本不会持续很长时间。

Removing the Content-Length header and changing .put() to .post() gives me results, in any case. 在任何情况下,删除Content-Length标头并将.put()更改为.post()都会给出结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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