无锋 发表于 2020-5-22 20:24:49

python54讲,豆瓣登录问题

由于豆瓣登录的维护更新,小甲鱼原来的答案已经不能使用了,我参考一位鱼油发的代码,代码如下:
import urllib.request
from http.cookiejar import CookieJar
import json

# 豆瓣的登录url
loginurl = 'https://accounts.douban.com/j/mobile/login/basic'
cookie = CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookie))
headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) \
         AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 \
         Safari/537.36"}

data = {
"ck":"",
"name":"这里填邮箱",
"password":"这里填密码",
"remember":"false",
"ticket":""
}

req = urllib.request.Request(loginurl,urllib.parse.urlencode(data).\
                           encode('utf-8'),headers)
res = opener.open(req)
res_html = res.read().decode('utf-8')
res_js = json.loads(res_html)
if res_js['status'] == 'success':
    print('登录成功')
else:
    print('登录失败')


输入正确的邮箱密码,运行后登录失败,返回的json显示参数缺失
{"status":"failed","message":"parameter_missing","description":"参数缺失","payload":{}}
麻烦解答一下,求求了!

suchocolate 发表于 2020-5-23 01:06:35

试试requests吧,免去设置cookie。
import requests

url = 'https://accounts.douban.com/j/mobile/login/basic'
headers = {'user-agent': 'firefox'}
data = {
    "ck": "",
    "name": "邮箱",
    "password": "密码",
    "remember": "false",
    "ticket": ""
}
s = requests.session()
r1 = s.get(url=url, headers=headers)
r2 = s.post(url=url, headers=headers, data=data)
print(r2.text)

无锋 发表于 2020-5-23 16:53:31

suchocolate 发表于 2020-5-23 01:06
试试requests吧,免去设置cookie。

用你的代码成功了,谢谢啦!受你的这一句代码"r1 = s.get(url=url, headers=headers)"启发,我找到问题所在了,需要先用get方式请求一次,再向登录的url传参,代码修改如下:import urllib.request
from http.cookiejar import CookieJar
import json

# 豆瓣的登录url
loginurl = 'https://accounts.douban.com/j/mobile/login/basic'
# 豆瓣首页url
url = 'https://www.douban.com/'
# 定制opener,传递cookie
cookie = CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookie))
# 设置headers
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) \
         AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 \
         Safari/537.36'}
# 表单提交参数设置
data = {
'ck':'',
'name':'这里填邮箱~QAQ',
'password':'这里填密码~QAQ',
'remember':'false',
'ticket':''
}
# get请求的request
req_get = urllib.request.Request(url,headers=headers)
# post请求的request
req_post = urllib.request.Request(loginurl,urllib.parse.urlencode(data).\
                           encode('utf-8'),headers)
# get请求的response
res_get = opener.open(req_get)
# post请求的response
res_post = opener.open(req_post)

res_html = res_post.read().decode('utf-8')

# json解码
res_js = json.loads(res_html)
# 根据status的值判断是否登录成功
if res_js['status'] == 'success':
    print('登录成功')
else:
    print('登录失败')

zwhe 发表于 2020-5-31 10:42:20

{:5_92:}

hhhhhhy 发表于 2020-8-11 21:00:16

无锋 发表于 2020-5-23 16:53
用你的代码成功了,谢谢啦!受你的这一句代码"r1 = s.get(url=url, headers=headers)"启发,我找到问题所 ...

还是登录失败orz

wangxaio 发表于 2020-11-15 09:38:34

无锋 发表于 2020-5-23 16:53
用你的代码成功了,谢谢啦!受你的这一句代码"r1 = s.get(url=url, headers=headers)"启发,我找到问题所 ...

不要需要用两次请求,参数缺失是因为 headers 中缺少 referer,添加这个参数就可以了

wangxaio 发表于 2020-11-15 09:39:17

无锋 发表于 2020-5-23 16:53
用你的代码成功了,谢谢啦!受你的这一句代码"r1 = s.get(url=url, headers=headers)"启发,我找到问题所 ...

不要需要用两次请求,参数缺失是因为 headers 中缺少 referer,添加这个参数就可以了

fc5igm 发表于 2021-6-20 08:06:03

suchocolate 发表于 2020-5-23 01:06
试试requests吧,免去设置cookie。

请问这样怎么破
{"status":"failed","message":"captcha_required","description":"需要图形验证码","payload":{"tc_app_id":"2044348370","captcha_signature_sample":"12:1,28:2","touch_cap_url":"https:\/\/ssl.captcha.qq.com\/TCaptcha.js","captcha_id":"login:captcha:B6UGW44lW8O9NATmd9op7Jrh","captcha_image_url":"https:\/\/accounts.douban.com\/j\/captcha\/show?vid=login:captcha:B6UGW44lW8O9NATmd9op7Jrh&size=small"}}

Vicissitude357 发表于 2022-9-8 17:55:27

还是失败
页: [1]
查看完整版本: python54讲,豆瓣登录问题