python爬虫问题登录豆瓣客户端
import reimport urllib.request
from http.cookiejar import CookieJar
loginurl = "https://www.douban.com/accounts/login"
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0;WOW64;rv:23.0) Gecko/20100101 Firefox/74.0'}
cookie = CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor)
data = {
"form_email":"your email",
"form_possword":"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 = 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\豆瓣登录.py", line 23, 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:
这个错误我在网上查了试了好多方法也没有解决 大神帮帮忙
加上headers
这个肯定可以百度到 qiuyouzhi 发表于 2020-3-31 18:25
加上headers
这个肯定可以百度到
我加了。。。上面代码就是加了的。
可以参考这里的代码:https://github.com/CharlesPikachu/DecryptLogin
页:
[1]