【问题标题】:Why does this basic urllib code work, but this basic requests code not?为什么这个基本的 urllib 代码可以工作,而这个基本的请求代码却不行?
【发布时间】:2014-09-30 18:21:04
【问题描述】:

我正在尝试让机器人登录 phpbb3 论坛,我可以在 urllib 中进行操作。但是,因为它需要会话 ID 等,所以当您更改页面时它不会保持登录状态(我认为这是问题所在)。所以我尝试使用requests,但我什至无法让requests 登录,即使使用urllib 很容易登录。

#!/usr/bin/env python3

import urllib
import http.cookiejar
from bs4 import BeautifulSoup

username = ''
password = ''

cj = http.cookiejar.CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
opener.addheaders = [('User-agent','Mozilla/5.0')]

auth_url = "http://www.mingeford365.co.uk/forum/ucp.php?mode=login"
payload  = {'username' : username, 'password' : password, 
            "autologin" : "on", 'login' : 'Login'}

data = urllib.parse.urlencode(payload)
binary_data = data.encode('UTF-8')

req = urllib.request.Request(auth_url,binary_data)
resp = urllib.request.urlopen(req)
contents = resp.read().decode('UTF-8')

if username in contents:
   print('logged in.')

上面的代码有效。以下请求代码不起作用

    #!/usr/bin/env python3
import requests
from bs4 import BeautifulSoup

url = 'http://www.mingeford365.co.uk/forum/ucp.php?mode=login'

logininfo = {'username': '',
             'password': '',
             'autologin' : "on", 
             'login' : 'Login'}

headers = {'User-Agent' : 'Mozilla/5.0 (x11; Ubuntu; Linux x86; rv:28.0) Gecko/20100101 Firefox/28.0'}
           #'Accept': 'text/html, application/xhtml+xhtml,application/xml;q=0.9,*/*;q=0.8',
           #'Accept-Language': 'en-gb,en;q=0.5', 
           #'Accept-Encoding':  'gzip, deflate',                                   
           #'referer': 'http://www.mingeford365.co.uk/forum/index.php',
           #'Connection' : 'keep-alive',
           #'Content-Type': 'application/x-www-form-urlencoded'}

session = requests.Session()
get_session_id = session.get("http://www.mingeford365.co.uk/forum",headers=headers)
print(get_session_id.status_code)

response = session.post(url,params=logininfo,headers=headers) #cookies=get_session_id.cookies
soup = BeautifulSoup(response.text)

print(soup.get_text())

【问题讨论】:

  • 你确定这是传递登录信息的正确方法
  • 您能否提供比“不起作用”更精确的问题陈述?

标签: python login urllib bots phpbb3


【解决方案1】:

您将 POST 正文参数放入您的 URL。使用data,而不是params

response = session.post(url, data=logininfo, headers=headers)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-28
    • 1970-01-01
    • 2016-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多