成功登录会话后,Python请求GET失败

时间:2014-03-23 10:45:12

标签: python web-scraping

所以我使用Python Requests库登录PHP-WebCMS。到目前为止,我能够使用带有效负载的post-command登录。我可以下载文件。

问题是:当我在登录后通过POST运行GET-Command时,它告诉我我已经不再登录了 - 虽然我还在使用相同的会话!请看一下代码

#Lets Login
with requests.session() as s:
payload = {'username': strUserName, 'password': strUserPass, 'Submit':'Login'}
r = s.post(urlToLoginPHP, data=payload, stream=True)
#Ok we are logged in. If I would run the #DOWNLOADING Files code right here I would get a correct zip file
#But since the r2-Get-Command delivers the "please login" page it doesn't work anymore
r2 = s.get("urlToAnotherPageOfThisWebsite",stream=True)
#We are not logged in anymore

#DOWNLOADING Files: This now just delivers a 5KB big file which contains the content
 of the "please login page"
local_filename = 'image.zip'
# NOTE the stream=True parameter
r1 = s.get(downloadurl, stream=True)
with open(local_filename, 'wb') as f:
    for chunk in r1.iter_content(chunk_size=1024): 
        if chunk: # filter out keep-alive new chunks
            f.write(chunk)
            f.flush()

1 个答案:

答案 0 :(得分:0)

我找到了解决方案:我登录浏览器并尝试同时通过python登录。该网站注意到我已经登录并且我误解了它。

相关问题