我正在尝试让机器人登录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())
答案 0 :(得分:0)
您将POST主体参数放在URL中。使用data
,而不是params
:
response = session.post(url, data=logininfo, headers=headers)