【发布时间】: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