【转】【Python】Python3爬虫实现自动登录、签到
工具:Fiddler
首先下载安装Fiddler,这个工具是用来监听网络请求,有助于你分析请求链接和参数。
打开目标网站:http://www.17sucai.com/,然后点击登录
好了,先别急着登录,打开你的Fiddler,此时Fiddler里面是没有监听到网络请求的,然后回到页面,输入邮箱和密码,点击登录,下面再到fiddler里面去看
这里面的第一个请求就是你点击登录的网络请求,点击这个链接可以在右边看到你的一些请求信息
然后点击WebForms可以看到你的请求参数,也就是用户名和密码
下面我们有代码来实现登录功能
import urllib.request
import urllib
import gzip
import http.cookiejar #定义一个方法用于生成请求头信息,处理cookie
def getOpener(head):
# deal with the Cookies
<pre name="code" class="python"> cj = http.cookiejar.CookieJar()
pro = urllib.request.HTTPCookieProcessor(cj)
opener = urllib.request.build_opener(pro)
header = []
for key, value in head.items():
elem = (key, value)
header.append(elem)
opener.addheaders = header
return opener #定义一个方法来解压返回信息
def ungzip(data):
try: # 尝试解压
print('正在解压.....')
data = gzip.decompress(data)
print('解压完毕!')
except:
print('未经压缩, 无需解压')
return data #封装头信息,伪装成浏览器
header = {
'Connection': 'Keep-Alive',
'Accept-Language': 'zh-CN,zh;q=0.8',
'Accept': 'application/json, text/javascript, */*; q=0.01',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36',
'Accept-Encoding': 'gzip, deflate',
'X-Requested-With': 'XMLHttpRequest',
'Host': 'www.17sucai.com',
} url = 'http://www.17sucai.com/auth'
opener = getOpener(header) id = 'xxxxxxxxxxxxx'#你的用户名
password = 'xxxxxxx'#你的密码
postDict = {
'email': id,
'password': password,
} postData = urllib.parse.urlencode(postDict).encode()
op = opener.open(url, postData)
data = op.read()
data = ungzip(data) print(data)
好了,接下来清空一下你的Fiddler,然后运行这个程序,看一下你的Fiddler
你可以点击这个链接,看看右边的请求信息和你用浏览器请求的是不是一样
下面是程序后代打印的信息
code=200表示登陆成功
解析来就需要获取到签到的url,这里你需要一个没有签到的账号在网站中点击签到按钮,然后通过Fiddler来获取到签到的链接和需要的信息。
然后点击“签到”,签到成功后到Fiddler中查看捕捉到的url
点击这个url可以在右边查看访问这个链接时所需要的头信息和cookies神马的,我们已经登录成功后直接使用cookies就行了,python对cookies的处理做好了封装,下面是我的代码中对cookies的使用
cj = http.cookiejar.CookieJar()
pro = urllib.request.HTTPCookieProcessor(cj)
opener = urllib.request.build_opener(pro)
下面是签到成功返回的信息:code=200表示请求成功,day=1表示连续签到一天,score=20表示获得的积分数
下面放出完整代码,当然,为了测试代码签到,你还需要你一没有签到过的账号
import urllib.request
import urllib
import gzip
import http.cookiejar def getOpener(head):
# deal with the Cookies
cj = http.cookiejar.CookieJar()
pro = urllib.request.HTTPCookieProcessor(cj)
opener = urllib.request.build_opener(pro)
header = []
for key, value in head.items():
elem = (key, value)
header.append(elem)
opener.addheaders = header
return opener def ungzip(data):
try: # 尝试解压
print('正在解压.....')
data = gzip.decompress(data)
print('解压完毕!')
except:
print('未经压缩, 无需解压')
return data header = {
'Connection': 'Keep-Alive',
'Accept-Language': 'zh-CN,zh;q=0.8',
'Accept': 'application/json, text/javascript, */*; q=0.01',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36',
'Accept-Encoding': 'gzip, deflate',
'X-Requested-With': 'XMLHttpRequest',
'Host': 'www.17sucai.com',
} url = 'http://www.17sucai.com/auth'
opener = getOpener(header) id = 'xxxxxxx'
password = 'xxxxxxx'
postDict = {
'email': id,
'password': password,
} postData = urllib.parse.urlencode(postDict).encode()
op = opener.open(url, postData)
data = op.read()
data = ungzip(data) print(data) url = 'http://www.17sucai.com/member/signin' #签到的地址 op = opener.open(url) data = op.read()
data = ungzip(data) print(data)
相比登录,签到也就是在登录完成后重新打开一个链接而已,由于我的账号都已经签到过了,这里就不在贴运行代码的图 了。
接下来要做的就是在你电脑上写个bat 脚本,再在“任务计划”中添加一个定时任务就行了。
在此之前你还需要配置一下python的环境变量,这里就不在赘述了。
原文链接:http://blog.csdn.net/u283056051/article/details/49946981
【转】【Python】Python3爬虫实现自动登录、签到的更多相关文章
- python+selenium+chrome实现自动登录百度
#python3.4+selenium3.5+chrome版本 63.0.3239.132+chrome驱动chromedriver.exe #实现自动登录百度 from selenium impor ...
- python paramiko模块SSH自动登录linux系统进行操作
1). Linux系统首先要开启SSH服务:service ssh status 如果没安装的话,则要:apt-get install openssh-server service ssh resta ...
- 【Python3爬虫】自动查询天气并实现语音播报
一.写在前面 之前写过一篇用Python发送天气预报邮件的博客,但是因为要手动输入城市名称,还要打开邮箱才能知道天气情况,这也太麻烦了.于是乎,有了这一篇博客,这次我要做的就是用Python获取本机I ...
- 精通python网络爬虫之自动爬取网页的爬虫 代码记录
items的编写 # -*- coding: utf-8 -*- # Define here the models for your scraped items # # See documentati ...
- python webdriver 显示等待-自动登录126邮箱,添加联系人
脚本内容:#encoding=utf-8#author-夏晓旭from selenium import webdriverimport timefrom selenium.webdriver.supp ...
- python自动登录代码
公司有很多管理平台,账号有禁用机制,每个月至少登录一次,否则禁用.导致有时候想登录某个平台的时候,发现账号已经被禁用了,还得走流程解禁.因此用python实现了一下自动登录,每天定时任务运行一次.ps ...
- [Python爬虫] Selenium实现自动登录163邮箱和Locating Elements介绍
前三篇文章介绍了安装过程和通过Selenium实现访问Firefox浏览器并自动搜索"Eastmount"关键字及截图的功能.而这篇文章主要简单介绍如何实现自动登录163邮箱,同时 ...
- python网络爬虫之使用scrapy自动登录网站
前面曾经介绍过requests实现自动登录的方法.这里介绍下使用scrapy如何实现自动登录.还是以csdn网站为例. Scrapy使用FormRequest来登录并递交数据给服务器.只是带有额外的f ...
- python爬虫+使用cookie登录豆瓣
2017-10-09 19:06:22 版权声明:本文为博主原创文章,未经博主允许不得转载. 前言: 先获得cookie,然后自动登录豆瓣和新浪微博 系统环境: 64位win10系统,同时装pytho ...
随机推荐
- github pull request
https://stackoverflow.com/questions/14680711/how-to-do-a-github-pull-request https://help.github.com ...
- 技术范儿的 Keep 发力AI赛道,为什么“虚拟教练”会更懂你?
http://www.tmtpost.com/3363367.html 摘要: 虚拟教练技术会整合到一些业务场景和硬件产品中收费,但是收费的具体情况彭跃辉还暂未透露. 图片来源于Unsplash 自去 ...
- 动态SQL中 实现条件参数 varchar类型的参数名称 以及模糊查询实现
set @strSQL='select * from testtable AS P WHERE P.Type='+@PType+' and P.PName ='''+@PName+''' and P. ...
- python(59):yield 函数
可迭代对象: 当你建立了一个列表,你可以逐项地读取这个列表,这叫做一个可迭代对象: >>> mylist = [1, 2, 3] >>> for i in myli ...
- linux命令(46):程序运行前后台切换
A,Shell支持作用控制,有以下命令:1. command& 让进程在后台运行2. jobs 查看后台运行的进程3. fg %n 让后台运行的进程n到前台来4. bg %n 让进程n到后台去 ...
- Delphi对象池MyObjectPool.pas
对象池一般在服务端使用,所以稳定性是第一的. 欢迎提意见 unit uMyObjectPool; interface uses SyncObjs, Classes, Windows, SysUtils ...
- 对JSON格式的城市按照拼音首字母排序
需求说明: App应用中最常见的一种操作就是对城市按照拼音首字母排序,以方便选择城市.而已有的json格式的城市数据是没有这种排序的. 已有的json格式的城市数据格式如下[简化之后]: 数据格式说明 ...
- libevent源码分析:event_add、event_del
event_add.event_del两个函数分别是使event生效和失效的,下面就来看一下两个函数的实现. event_add int event_add(struct event *ev, con ...
- 前端js上传文件插件
1. plupload文件上传 2.ajaxfileupload文件上传
- 【Ensemble methods】组合方法&集成方法
机器学习的算法中,讨论的最多的是某种特定的算法,比如Decision Tree,KNN等,在实际工作以及kaggle竞赛中,Ensemble methods(组合方法)的效果往往是最好的,当然需要消耗 ...