|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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(cookie))
head = {}
head["User-Agent"] = "这里改了"
data = {}
data['form_email'] = '这里填了'
data['form_password'] = '这里也填了'
data['source'] = 'index_nav'
response = opener.open(loginurl, urllib.parse.urlencode(data).encode('utf-8'), head)
#验证成功跳转至登录页
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'), head)
# 登录成功跳转至首页 '''
if response.geturl() == "http://www.douban.com/":
print('登录成功!')
报错↓
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\54.1.py", line 22, in <module>
response = opener.open(loginurl, urllib.parse.urlencode(data).encode('utf-8'), head)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 525, in open
response = self._open(req, data)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 542, in _open
result = self._call_chain(self.handle_open, protocol, protocol +
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 502, in _call_chain
result = func(*args)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 1393, in https_open
return self.do_open(http.client.HTTPSConnection, req,
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 1350, in do_open
h.request(req.get_method(), req.selector, req.data, headers,
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\http\client.py", line 1255, in request
self._send_request(method, url, body, headers, encode_chunked)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\http\client.py", line 1301, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\http\client.py", line 1250, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\http\client.py", line 1010, in _send_output
self.send(msg)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\http\client.py", line 950, in send
self.connect()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\http\client.py", line 1417, in connect
super().connect()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\http\client.py", line 921, in connect
self.sock = self._create_connection(
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\socket.py", line 793, in create_connection
sock.settimeout(timeout)
TypeError: an integer is required (got type dict) |
|