关于054讲爬虫的自我修养动手题1问题
各位老师我使用谷歌浏览器的检查网页功能登录豆瓣的账号。method都是get请求。没有post请求这种情况该怎么处理。这样我是不是就没法完成作业了,这是老师的答案。如何替换上面的内容才能正常登录豆瓣。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('登录成功!')
页:
[1]