如何直接在Python中发布请求体?
我有以下关于POST请求的详细信息如何直接在Python中发布请求体?,python,html,post,http-post,Python,Html,Post,Http Post,我有以下关于POST请求的详细信息 1)URL:“http://kuexams.org/get_results“ 2)请求正文:“htno=001111505&ecode=kuBA3\u Supply\u Dec\u 2013”。 我是通过分析HTTP流量得到的。然后我找到了这个站点,您在其中指定了这些值,然后它重新返回响应 我需要知道如何制作类似的东西。只需将请求正文发布到URL并返回数据进行解析。 import mechanize import cookielib import urllib
1)URL:“http://kuexams.org/get_results“
2)请求正文:“htno=001111505&ecode=kuBA3\u Supply\u Dec\u 2013”。
我是通过分析HTTP流量得到的。然后我找到了这个站点,您在其中指定了这些值,然后它重新返回响应
我需要知道如何制作类似的东西。只需将请求正文发布到URL并返回数据进行解析。
import mechanize
import cookielib
import urllib
import logging
import sys
import re
br = mechanize.Browser()
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
br.set_handle_equiv(True)
br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=3)
br.addheaders = [('User-agent', 'Firefox')]
r = br.open('http://kuexams.org/results/06JZ4SYmTV97s4oROGuLYglPFH3XxJKAunIilJkDBV0gBxSU6YVJ_kXRL0UZb3cIjz9aFdnkYaE-T_S3ubaXPg,,/ugresults/ug')
scrape = r.read()
#print(scrape)
pattern = re.compile('=5&code=(.{5})\'')
img = pattern.findall(scrape)
print(img)
imgstr = str()
imgstr = img[0]
print(imgstr)
br.select_form("appForm")
br.form["htno"] ='001111441'
br.form["entered_captcha"] = imgstr
response = br.submit()
print response.read()
br.back()
p.S:我尝试了多种方法发布到该站点
这是我最累的。它使用不同的方法,但失败惨重。
import mechanize
import cookielib
import urllib
import logging
import sys
import re
br = mechanize.Browser()
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
br.set_handle_equiv(True)
br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=3)
br.addheaders = [('User-agent', 'Firefox')]
r = br.open('http://kuexams.org/results/06JZ4SYmTV97s4oROGuLYglPFH3XxJKAunIilJkDBV0gBxSU6YVJ_kXRL0UZb3cIjz9aFdnkYaE-T_S3ubaXPg,,/ugresults/ug')
scrape = r.read()
#print(scrape)
pattern = re.compile('=5&code=(.{5})\'')
img = pattern.findall(scrape)
print(img)
imgstr = str()
imgstr = img[0]
print(imgstr)
br.select_form("appForm")
br.form["htno"] ='001111441'
br.form["entered_captcha"] = imgstr
response = br.submit()
print response.read()
br.back()
我可以看到post请求没有使用任何cookie或验证码细节。它只在客户端实现,因此不需要存储cookie或获取验证码,因此此代码适用于您使用请求它既酷又易用
import requests
url='http://kuexams.org/get_results'
payload={'htno': '001111441', 'ecode': 'kuBA2_Supply_Dec_2013'}
headers={"User-Agent": "Some Cool Thing"}
r=requests.post(url,headers=headers,data=payload)
print r.content
请注意,服务器不接受默认的用户代理请求,因此我添加了一个自定义的用户代理,否则它将不接受POST请求或可能给出禁止的错误。您的问题是什么?你没问过这件事。@aIKid我下面的代码不起作用,也没有必要。我希望能够使用URL和请求体发布文章,就像我提到的网站一样(由另一个Stack exchange成员制作,用于测试文章)。您得到了什么样的响应?是否引发了异常?@aIKid我不知道结果。它只是给我的原始网站,好像什么都没有发布。此外,为什么要经历所有这些寻找卡帕查剧本的无稽之谈呢。我得到了直邮链接。当我在“”(这是一个测试POST和其他HTTP请求的站点)上测试它时,它可以完美地工作。但我自己不知道如何实现这一点。