# 模拟登录豆瓣

from urllib.request import urlretrieve
import requests
from bs4 import BeautifulSoup
from os import remove
try:
import cookielib
except BaseException:
import http.cookiejar as cookielib
try:
from PIL import Image
except BaseException:
pass url = 'https://accounts.douban.com/login' datas = {'source': 'index_nav',
'remember': 'on'} headers = {
'Referer': 'https://www.douban.com/',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
' (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Language': 'zh-CN,zh;q=0.8'} # 尝试使用cookie信息
session = requests.session()
session.cookies = cookielib.LWPCookieJar(filename='cookies')
try:
session.cookies.load(ignore_discard=True)
except BaseException:
print("Cookies未能加载")
# cookies加载不成功,则输入账号密码信息
datas['form_email'] = input('Please input your account:')
datas['form_password'] = input('Please input your password:') def get_captcha():
'''
获取验证码及其ID
'''
r = requests.post(url, data=datas, headers=headers)
page = r.text
soup = BeautifulSoup(page, "html.parser")
# 利用bs4获得验证码图片地址
img_src = soup.find('img', {'id': 'captcha_image'}).get('src')
urlretrieve(img_src, 'captcha.jpg')
try:
im = Image.open('captcha.jpg')
im.show()
im.close()
except BaseException:
print('到本地目录打开captcha.jpg获取验证码')
finally:
captcha = input('please input the captcha:')
remove('captcha.jpg')
captcha_id = soup.find(
'input', {'type': 'hidden', 'name': 'captcha-id'}).get('value')
return captcha, captcha_id def isLogin():
'''
通过查看用户个人账户信息来判断是否已经登录
'''
url = "https://www.douban.com/accounts/"
login_code = session.get(url, headers=headers,
allow_redirects=False).status_code
if login_code == 200:
return True
else:
return False def login():
captcha, captcha_id = get_captcha()
# 增加表数据
datas['captcha-solution'] = captcha
datas['captcha-id'] = captcha_id
login_page = session.post(url, data=datas, headers=headers)
page = login_page.text
soup = BeautifulSoup(page, "html.parser")
result = soup.findAll('div', attrs={'class': 'title'})
# 进入豆瓣登陆后页面,打印热门内容
for item in result:
print(item.find('a').get_text())
# 保存 cookies 到文件,
# 下次可以使用 cookie 直接登录,不需要输入账号和密码
session.cookies.save() if __name__ == '__main__':
if isLogin():
print('Login successfully')
else:
login()

19.SimLogin_case07的更多相关文章

  1. Mediaplayer error (-19,0)

    Android MediaPlayer 发生 error (-19,0) 错误解决方法. 引起原因:由于多次实例化MediaPlayer.start() 进行播放操作引起的.由于没有及时释放内存资源导 ...

  2. 录像时调用MediaRecorder的start()时发生start failed: -19错误

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  3. CSharpGL(19)用glReadPixels把渲染的内容保存为PNG图片(C#)

    CSharpGL(19)用glReadPixels把渲染的内容保存为PNG图片(C#) 效果图 本文解决了将OpenGL渲染出来的内容保存到PNG图片的方法. 下载 CSharpGL已在GitHub开 ...

  4. ABP(现代ASP.NET样板开发框架)系列之19、ABP应用层——审计日志

    点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之19.ABP应用层——审计日志 ABP是“ASP.NET Boilerplate Project (ASP.NET ...

  5. js正则表达式校验非负浮点数:^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. js正则表达式校验非负整数:^\d+$ 或 ^[1-9]\d*|0$

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. js正则表达式校验非零的正整数:^[1-9]\d*$ 或 ^([1-9][0-9]*){1,3}$ 或 ^\+?[1-9][0-9]*$

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. C#开发微信门户及应用(19)-微信企业号的消息发送(文本、图片、文件、语音、视频、图文消息等)

    我们知道,企业号主要是面向企业需求而生的,因此内部消息的交流显得非常重要,而且发送.回复消息数量应该很可观,对于大企业尤其如此,因此可以结合企业号实现内部消息的交流.企业号具有关注安全.消息无限制等特 ...

  9. 19-typedef

    本文目录 一.typedef作用简介 二.typedef与指针 三.typedef与结构体 三.typedef与指向结构体的指针 四.typedef与枚举类型 五.typedef与指向函数的指针 六. ...

随机推荐

  1. JUC 一 ReentrantLock 可重入锁

    java.util.concurrent.locks ReentrantLock即可重入锁,实现了Lock和Serializable接口 ReentrantLock和synchronized都是可重入 ...

  2. Eureka的表兄弟Zookeeper理论基础

    Eureka的表兄弟Zookeeper 简单介绍 Zookeeper是一个开源的分布式应用程序协调服务器,主要功能包括配置维护,域名服务,分布式同步,集群管理等 主要功能简介 一.配置维护 分布式系统 ...

  3. smarty基础总结

    前提: 1. 部署smarty模板目录: 2. 编写Smarty类的子类,定制好template_dir.compile_dir.config_dir.cache_dir.left_delimiter ...

  4. css内容超出显示省略号

    CSS实现单行.溢出显示省略号(…) 把要设置的显示省略号的标签,加上以下的属性 overflow: hidden; /*超出不显示*/ text-overflow: ellipsis;/* 超出内容 ...

  5. Java设计模式简单总结

    1.单例模式:在应用程序整个生命周期中,单例类的实例只有一个,并且会自动实例化.单例类的构造方法必须为私有,并且提供一个全局访问点 public class Test { private Test() ...

  6. day26-多态、封装、反射

    #!/usr/bin/env python # -*- coding:utf-8 -*- # ----------------------------------------------------- ...

  7. 干货:Java技术栈18年02月精彩好文汇总

    一晃眼,一个月过去了,新年也过完了. 一晃眼,又老了一岁了,感觉新的一年压力更大了. 我们也该为这一个月的技术做做汇总了,错过的同学正好可以统一回顾看看,已经看过的同学也可以再温习一下.. Java技 ...

  8. 5.1_springboot2.x与安全(spring security)

    1.简介 常见的两个安全框架shiro|spring security,这里只介绍spring security; Spring Security是针对Spring项目的安全框架,也是Spring B ...

  9. 将excel表格或csv转换为Shapefile文件

    读取csv转为shp 构造读取csv函数 def read_csv(fp): ret = [] with open(fp, 'rb') as f: for line in f: ret.append( ...

  10. 自学之linux的基本命令

    cd cd 用于进入指定文件夹 cd ..用于回到上个文件夹 ls ls用于列出文件夹里的所有元素 ls/home/ 列出home文件夹的元素 ls -l 可以看到文件名,拥有者是谁,什么时候修改的 ...