0
我试图让一个机器人登录到phpbb3论坛,我可以在urllib
。但是,因为它需要会话ID等,所以在更改页面时它不会保持登录状态(我认为这是问题)。所以我试图使用requests
,但我甚至无法登录requests
,即使它易于使用urllib
登录。为什么这个基本的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())
你确定你是要通过将LoginInfo – 2014-09-30 10:12:50
你能提供更精确的问题陈述不是*“不起作用” *的正确方法? – jonrsharpe 2014-09-30 10:15:18