(60)-- 用程序改写豆瓣会员签名
程序员文章站
2022-05-02 17:43:49
...
# 用程序改写自己豆瓣签名
from urllib import request,parse
from http import cookiejar
import re
cookie = cookiejar.CookieJar()
cookie_handler = request.HTTPCookieProcessor(cookie)
opener = request.build_opener(cookie_handler)
# 替换默认openner
request.install_opener(opener)
# 获取登录页面
def getPage():
login_page = 'https://accounts.douban.com/login'
response = request.urlopen(login_page)
html = response.read().decode('utf-8')
if 'captcha_image' in html:
print('有验证码')
id_pat = re.compile(r'name="captcha-id" value="(.*?)"')
res = id_pat.search(html)
captcha_id = res.group(1)
image_pat = re.compile(r'id="captcha_image" src="(.*?)"')
res = image_pat.search(html)
captcha_image = res.group(1)
request.urlretrieve(captcha_image,'douban.jpg')
captcha_image = input('请输入验证码:')
print(captcha_id)
print(captcha_image)
login(captcha_id,captcha_image)
else:
print('无验证码')
# 登录
def login(id,image):
login_url = 'https://accounts.douban.com/login'
form = {
'form_email' : '[email protected]',
'form_password' : 'add56789',
'login' : '登录',
'captcha-solution' : image,
'captcha-id' : id,
'redir': 'https://www.douban.com/people/176100282/',
'source' : 'None'
}
print(form)
form = parse.urlencode(form)
headers = {
"Content-Length": len(form),
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.4620.400 QQBrowser/9.7.13014.400",
}
req = request.Request(url=login_url,data=bytes(form,encoding='utf-8'),headers=headers)
response = request.urlopen(req)
print(response.read().decode('utf-8'))
def edit_sig():
home_url = 'https://www.douban.com/people/176100282/'
response = request.urlopen(home_url)
html = response.read().decode()
ck_pat = re.compile(r'name="ck" value="(.*?)"')
res = ck_pat.search(html)
ck = res.group(1)
sig = input('输入签名:')
edit_url = 'https://www.douban.com/j/people/176100282/edit_signature'
form = {
'ck' : ck,
'signature' : sig
}
form = parse.urlencode(form)
headers = {
# "Host": "www.douban.com",
"Connection": "keep-alive",
"Content-Length": len(form),
"Accept": "application/json, text/javascript, */*; q=0.01",
"Origin": "https://www.douban.com",
"X-Requested-With": "XMLHttpRequest",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.4620.400 QQBrowser/9.7.13014.400",
"Content-Type": "application/x-www-form-urlencoded",
"Referer": "https://www.douban.com/people/176100282/",
"Accept-Language": "zh-CN,zh;q=0.9",
}
req = request.Request(url=edit_url, data=bytes(form, encoding='utf-8'), headers=headers)
response = request.urlopen(req)
print(response.read().decode())
if __name__ == '__main__':
getPage()
edit_sig()
# 因该号多次被用已封,可换新号一试
兄弟连学python
Python学习交流、资源共享群:563626388 QQ
推荐阅读