使用requests modu发布数据时出现400个错误请求

2024-09-29 21:46:35 发布

您现在位置:Python中文网/ 问答频道 /正文

标题说明了一切,奇怪的是没有数据的GET请求和POST请求不会导致此错误发生。我想我发布的数据可能有问题,不过还不太确定。顺便说一句,我使用cfscrape模块(Re:https://github.com/Anorov/cloudflare-scrape)绕过Cloudflare DDoS保护加载页面。总之,代码如下:

payload = { "username" : "testusername", "password" : "testpassword", "code" : "1111", "submit" : "Login" }
scraper = cfscrape.create_scraper()
res = scraper.post("http://www.umggaming.com/user/login", data=payload)
print res.content

我在发布数据时得到的回应是

^{pr2}$

如前所述,其他请求,即GET请求和POST请求,不包含数据,用html源代码成功响应。我现在很困惑。希望有人能帮我。谢谢您。在

@阿摩:

url = 'http://umggaming.com/user/login'
values = { 'username' : 'testusername', 'password' : 'testpassword', 'code' : '1111', 'submit' : 'Login' }
heads = { 
    'User-Agent' : 'Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 (FM Scene 4.6.1)', 
    'Referer' : 'http://www.umggaming.com/user/login',
    'Cookie' : '__cfduid=EXAMPLE; cf_clearance=EXAMPLE'
}
data = urllib.urlencode(values)
req = urllib2.Request(url, headers=heads, data=data)
response = urllib2.urlopen(req)
result = response.read()
print result

编辑:我可以用上面的代码绕过Cloudflare,但是在发送POST请求时没有得到预期的响应。如果提交的用户名/密码/代码对无效,则该页面将响应“抱歉,您的密码或代码不正确。”—我没有收到该响应。你知道为什么会这样吗?在


Tags: 数据代码comhttpdatagetlogin页面
2条回答

老问题,但既然这是一个顶级的谷歌搜索结果,我会提供什么修复它,同时尝试实现一个非常相似的结果。在

其实很简单:如果您还没有“授权”,Cloudflare似乎不会接受post请求。这意味着您首先必须对该域发出一个简单的GET请求,让cfscrape绕过它,然后您就可以POST做任何事情了。在

上述代码的一个简单修复方法是:

payload = { "username" : "testusername", "password" : "testpassword", "code" : "1111", "submit" : "Login" }
url = "http://www.umggaming.com/user/login"

scraper = cfscrape.create_scraper()
scraper.get(url) # Make a simple GET request to bypass CloudFlare and get the authorisation cookies first
res = scraper.post("http://www.umggaming.com/user/login", data=payload)

print res.content

我用Python测试了你的问题urllib.和我发现:

If you dont have a cookie of this web, it gives you a 503 code and a cookie __cfduid.

During the 503 page ,it send a request /cdn-cgi/l/chk_jschl?xxxxx to get another cookie cf_clearance

Then you can use these two cookies and a referer in your header to send the post form.

相关问题 更多 >

    热门问题