目标:1、模拟登录豆瓣,2、自动更改签名和发表说说。

代码如下:

 #!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'ziv·chan' import re
import time
import requests
from PIL import Image url = 'https://www.douban.com/' headers = {
'origin' : 'https://www.douban.com',
'referer' : 'https://www.douban.com/',
'user-agent' : 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36'
} session = requests.session() # 获取验证码Id
def get_captchaId():
html = session.get(url,headers=headers)
html.encoding = 'utf-8'
pageCode = html.text
pattern = re.compile('captcha[?]id=(.*?)&',re.S)
captchaId = re.findall(pattern,pageCode)[0]
return captchaId # 获取验证码图片
def get_captchaSolu(captchaId):
url_plus = 'https://www.douban.com/misc/captcha?id=%s&size=s' % captchaId
cha = session.get(url_plus,headers=headers)
with open('cap_douban.png','wb') as f:
f.write(cha.content)
f.close()
im = Image.open('cap_douban.png')
im.show()
im.close()
captcha = raw_input('请输入验证码')
return captcha # 编辑签名
def edit_signature(ck):
url = 'https://www.douban.com/j/people/143780683/edit_signature'
data = {
'ck' : ck,
'signature' : '人间有味是清欢。'
}
headers = {
'origin' : 'https://www.douban.com',
'referer' : 'https://www.douban.com/people/143780683/',
'user-agent' : 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36',
'x-requested-with' : 'XMLHttpRequest'
}
session.post(url,data=data,headers=headers) # 编辑说说
def edit_words(ck):
url = 'https://www.douban.com/'
data = {
'ck' : ck,
'comment' : '天之涯,地之角,知交半零落。一斛浊酒尽余欢,今宵别梦寒。'
}
headers = {
'origin': 'https://www.douban.com',
'referer': 'https://www.douban.com/',
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36'
}
session.post(url,data=data,headers=headers) # 获取ck
def get_ck():
url = 'https://www.douban.com/people/143780683/'
html = session.get(url,headers=headers)
pageCode = html.text
# print pageCode
pattern = re.compile('ck=(.*?)[(">)]',re.S)
ck = re.findall(pattern,pageCode)[0]
return ck if __name__ == '__main__':
captchaId = get_captchaId()
captcha = get_captchaSolu(captchaId)
form_data = {
'source' : 'index_nav',
'form_email' : '',
'form_password' : 'ChelseaFC.2',
'captcha-solution' : captcha,
'captcha-id' : captchaId
}
session.post('https://www.douban.com/accounts/login',data=form_data,headers=headers) ck = get_ck()
edit_signature(ck)
# 避免冲突,设置延时
time.sleep(5)
edit_words(ck)

Python模拟登录实战(二)的更多相关文章

  1. Python模拟登录实战(一)

    今天,学习了模拟登录新浪微博.模拟登录主要有两种方式,一.利用Cookie:二.模仿浏览器的请求,发送表单. 法一: Cookie:指某些网站为了辨别用户身份而储存在用户本地终端上的数据(通常经过加密 ...

  2. Python模拟登录实战(三)

    目标:模拟登录知乎 代码如下: #!/usr/bin/env python # -*- coding:utf-8 -*- __author__ = 'ziv·chan' import re impor ...

  3. Python入门:模拟登录(二)或注册之requests处理带token请求

    转自http://blog.csdn.net/foryouslgme/article/details/51822209 首先说一下使用Python模拟登录或注册时,对于带token的页面怎么登录注册模 ...

  4. 【Python数据分析】Python模拟登录(一) requests.Session应用

    最近由于某些原因,需要用到Python模拟登录网站,但是以前对这块并不了解,而且目标网站的登录方法较为复杂, 所以一下卡在这里了,于是我决定从简单的模拟开始,逐渐深入地研究下这块. 注:本文仅为交流学 ...

  5. 【py登陆】python模拟登录

    用Python模拟登录网站 前面简单提到了 Python 模拟登录的程序,但是没写清楚,这里再补上一个带注释的 Python 模拟登录的示例程序.简单说一下流程:先用cookielib获取cookie ...

  6. 忘记秘密利用python模拟登录暴力破解秘密

    忘记秘密利用python模拟登录暴力破解秘密: #encoding=utf-8 import itertools import string import requests def gen_pwd_f ...

  7. [Python] Python 模拟登录,并请求

    Python 模拟登录,并请求 # encoding: utf- import requests import socket import time socket.setdefaulttimeout( ...

  8. Python爬虫-百度模拟登录(二)

    上一篇-Python爬虫-百度模拟登录(一) 接上一篇的继续 参数 codestring codestring jxG9506c1811b44e2fd0220153643013f7e6b1898075 ...

  9. Python模拟登录的几种方法

    目录 方法一:直接使用已知的cookie访问 方法二:模拟登录后再携带得到的cookie访问 方法三:模拟登录后用session保持登录状态 方法四:使用无头浏览器访问 正文 方法一:直接使用已知的c ...

随机推荐

  1. js中的隐式转换

    js中的不同的数据类型之间的比较转换规则如下: 1. 对象和布尔值比较 对象和布尔值进行比较时,对象先转换为字符串,然后再转换为数字,布尔值直接转换为数字 [] == true; //false [] ...

  2. HDU 3572 最大流

    [题意]有n个任务,每个任务必须开始于第Si天之后(包括Si),结束于第Ei天之前(包括Ei),每个任务持续的时间为Pi,现在有m台机器,每台每天只能专注做其中一件任务,每个任务做的时间可以不连续.问 ...

  3. 手势识别 GestureDetector ScaleGestureDetector

    识别器GestureDetector基本介绍 当用户触摸屏幕的时候,会产生许多手势,例如down,up,scroll,filing等.一般情况下,我们可以通过View或Activity的onTouch ...

  4. ProGuard 代码混淆

    简介 Java代码是非常容易反编译的.为了很好的保护Java源代码,我们往往会对编译好的class文件进行混淆处理. ProGuard是一个混淆代码的开源项目.它的主要作用就是混淆,当然它还能对字节码 ...

  5. Android--------使用gson解析json文件

    ##使用gson解析json文件 **json的格式有两种:** **1. {}类型,及数据用{}包含:** **2. []类型,即数据用[]包含:** 下面用个例子,简单的介绍gson如何解析jso ...

  6. mysql数据库编码

    MySQL数据库默认的编码是: character set :latin1 collation : latin1_swedish_ci 查看MySQL支持的编码: mysql> show cha ...

  7. 自定义圆形imageview

    import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapShader ...

  8. 华为S5300交换机配置基于VLAN的本地端口镜像

    配置思路 1.  将Ethernet0/0/20接口配置为观察端口(监控端口) 2.  将VLAN 1.11.12.13.14配置为镜像VLAN 配置步骤 1.  配置观察端口 <Switch& ...

  9. iOS9之后对于NSURL的编码转换方法变化说明

    在iOS9之后,官方推荐使用下面的方法对NSString进行转换 - (nullable NSString *)stringByAddingPercentEncodingWithAllowedChar ...

  10. Mysql 锁粒度

    表锁: 表锁是mysql 中最几本的锁策略,并且是开销最小的策略:它会锁定整张表. 一个用户在对表进行锁操作(增,删,改)前,首先要获得写锁,这会阻塞其他用户对该表的所有读写操作.只有没有写锁时,其他 ...