马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import re
import urllib.request
from http.cookiejar import CookieJar
# 豆瓣的登录url
loginurl = 'https://www.douban.com/accounts/login'
cookie = CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor)
data = {
"form_email":"your email",
"form_password":"your password",
"source":"index_nav"
}
data = {}
data['form_email'] = '你的账号'
data['form_password'] = '你的密码'
data['source'] = 'index_nav'
response = opener.open(loginurl, urllib.parse.urlencode(data).encode('utf-8'))
#验证成功跳转至登录页
if response.geturl() == "https://www.douban.com/accounts/login":
html = response.read().decode()
#验证码图片地址
imgurl = re.search('<img id="captcha_image" src="(.+?)" alt="captcha" class="captcha_image"/>', html)
if imgurl:
url = imgurl.group(1)
# 将验证码图片保存至同目录下
res = urllib.request.urlretrieve(url, 'v.jpg')
# 获取captcha-id参数
captcha = re.search('<input type="hidden" name="captcha-id" value="(.+?)"/>' ,html)
if captcha:
vcode = input('请输入图片上的验证码:')
data["captcha-solution"] = vcode
data["captcha-id"] = captcha.group(1)
data["user_login"] = "登录"
# 提交验证码验证
response = opener.open(loginurl, urllib.parse.urlencode(data).encode('utf-8'))
# 登录成功跳转至首页 '''
if response.geturl() == "http://www.douban.com/":
print('登录成功!')
Traceback (most recent call last):
File "C:/Users/Administrator/AppData/Local/Programs/Python/Python38/111.py", line 20, in <module>
response = opener.open(loginurl, urllib.parse.urlencode(data).encode('utf-8'))
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 531, in open
response = meth(req, response)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 640, in http_response
response = self.parent.error(
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 569, in error
return self._call_chain(*args)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 502, in _call_chain
result = func(*args)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 649, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 418:
一大堆的红字 |