# -*- coding: utf-8 -*-
"""
Created on Wed Jun 6 13:18:58 2018 @author: Lenovo
""" # -*- coding: utf-8 -*- import requests
import urllib
import random
from datetime import datetime
# python2 和 python3的兼容代码
try:
# python2 中
import cookielib
print(f"user cookielib in python2.")
except:
# python3 中
import http.cookiejar as cookielib
print(f"user cookielib in python3.") # session代表某一次连接
huihuSession = requests.session()
# 因为原始的session.cookies 没有save()方法,所以需要用到cookielib中的方法LWPCookieJar,这个类实例化的cookie对象,就可以直接调用save方法。
huihuSession.cookies = cookielib.LWPCookieJar(filename = "huihuCookies.txt") userAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36"
header = {
# "origin": "https://passport.huihu.cn",
"Referer": "http://hh.haiper.com.cn/w/wander/user/login/",
'User-Agent': userAgent,
} def huihuLogin(account, password):
#
print ("开始模拟登录嘻嘻嘻") postUrl = "http://hh.haiper.com.cn/w/wander/user/login/"
postData = {
"username": account,
"password": password,
} # 使用session直接post请求
responseRes = huihuSession.post(postUrl, data = postData, headers = header)
# 无论是否登录成功,状态码一般都是 statusCode = 200
#responseRes = requests.post(postUrl, data = postData, headers = header)
# 无论是否登录成功,状态码一般都是 statusCode = 200
print(f"statusCode = {responseRes.status_code}")
#print(f"text = {responseRes.text}")
huihuSession.cookies.save()
def isLoginStatus():
# 通过访问个人中心页面的返回状态码来判断是否为登录状态
for i in range(2131,2134):
routeUrl = "http://hh.haiper.com.cn/w/bench/extend/health/trade/all?nickname=&type=&gender=&level=&range%5Bstart%5D=2014-11-11+14%3A57&range%5Bend%5D=2018-06-06+14%3A57&page="+str(i) # 下面有两个关键点
# 第一个是header,如果不设置,会返回500的错误
# 第二个是allow_redirects,如果不设置,session访问时,服务器返回302,
# 然后session会自动重定向到登录页面,获取到登录页面之后,变成200的状态码
# allow_redirects = False 就是不允许重定向
try:
responseRes = huihuSession.get(routeUrl, headers = header, allow_redirects = False)
result = responseRes.text
except:
continue
start = result.find('<div class="form-control-static form-control-static-list">')
result = result[start:]
#print (result)
for j in range(1,16):
start = result.find('擦擦擦图片')
if start==-1:
break
else:
result = result[start:]
start = result.find('src="')
result = result[start+5:]
end = result.find('" class="img-rounded"')
imgpath = result[:end]
print (imgpath)
if imgpath=='/attachment/':
continue
randomname = datetime.now().strftime("%Y%m%d_%H%M%S") + str(random.randint(1,100))+'.jpg'
try:
urllib.request.urlretrieve(imgpath,'./擦擦擦/'+randomname)
except:
continue
print (i)
print(f"isLoginStatus = {responseRes.status_code}")
#print(f"text = {responseRes.text}")
if responseRes.status_code != 200:
return False
else:
return True
if __name__ == "__main__":
# 从返回结果来看,有登录成功
huihuLogin("xxxx", "xxxx")
isLogin1 = isLoginStatus()
print(f"is login huihu = {isLogin1}")

Python爬虫带用户名密码登录的更多相关文章

  1. Python用户名密码登录系统(MD5加密并存入文件,三次输入错误将被锁定)及对字符串进行凯撒密码加解密操作

    # -*- coding: gb2312 -*- #用户名密码登录系统(MD5加密并存入文件)及对字符串进行凯撒密码加解密操作 #作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.co ...

  2. python爬虫scrapy框架——人工识别登录知乎倒立文字验证码和数字英文验证码(2)

    操作环境:python3 在上一文中python爬虫scrapy框架--人工识别知乎登录知乎倒立文字验证码和数字英文验证码(1)我们已经介绍了用Requests库来登录知乎,本文如果看不懂可以先看之前 ...

  3. git clone 带用户名密码的形式但包含@等特殊符号无法正常解析

    正常使用git clone 的方式 git clone https://remote 使用带用户名密码的方式(可以避免后续每次都要输入用户名密码) git clone https://[usernam ...

  4. pyhton学习,day1作业,用户名密码登录模块

    要求,通过用户名密码登录,登录错误3次,锁定用户名 # coding=utf-8 # Author: RyAn Bi import os, sys #调用系统自己的库 accounts_file = ...

  5. 【Python练习】文件引用用户名密码登录系统

    一.通过txt文件引入用户名密码 1 #coding=utf-8 from selenium import webdriver #from selenium.common.exceptions imp ...

  6. [Python爬虫] Selenium实现自动登录163邮箱和Locating Elements介绍

    前三篇文章介绍了安装过程和通过Selenium实现访问Firefox浏览器并自动搜索"Eastmount"关键字及截图的功能.而这篇文章主要简单介绍如何实现自动登录163邮箱,同时 ...

  7. Python实现LDAP用户名密码验证

    网上借鉴了不少东西,下面是python代码,备份后用. 思路,因为每个用户的组都不一样,这样就导致了dn不一致的情况, 据需要先根据用户名获取该用户的dn,然后再bind用户名和密码进行验证. 反正是 ...

  8. 用户名密码登录小程序及input与raw_input区别。

    一.此次程序需要实现: 1.设定固定的用户名密码 2.用户名密码输入正确打印登录正确信息 3.仅仅运行三次登录 二.本次使用的python版本为: Windows下版本号: C:\Users\dais ...

  9. Python爬虫之用脚本登录Github并查看信息

    前言分析目标网站的登录方式 目标地址:https://github.com/login     登录方式做出分析: 第一,用form表单方式提交信息, 第二,有csrf_token, 第三 ,是以po ...

随机推荐

  1. postman批量接口测试注意事项

    1.使用cvs文件 导入文件后最后行出现\r符号 用文本打开 删除最后一行空白行 2.打印cvs文件中的接口调用的参数 Pre-request Script: var beginDate=data.b ...

  2. 跟踪LinkedList源码,通过分析双向链表实现原理,自定义一个双向链表

    1.LinkedList实现的基本原理 LinkedList是一个双向链表,它主要有两个表示头尾节点的成员变量first  .last,因其有头尾两个节点,所以从头或从尾操作数据都非常容易快捷.Lin ...

  3. rabbit-mq cluster安装

    Centos6.5 安装 RabbitMQ3.6.5 一.安装编译工具 yum -y install make gcc gcc-c++ kernel-devel m4 ncurses-devel op ...

  4. jquery时钟

    <script type="text/javascript"> function getDate(){ var mydate = new Date(); //时间对象 ...

  5. dfs--汉诺塔

    在研究汉诺塔问题时,我们可以先分析俩个盘子的方法: 1.把第一个盘子放到辅助柱子上 2.把第二个盘子放大目标柱子上 3.把第一个盘子从辅助柱子移到目标柱子上 由此我们可以通过整体思想推导出一共有n个盘 ...

  6. PHP学习之-文件上传

    一.PHP文件上传 HTML部分 <form action="file_big.php" method="post" enctype="mult ...

  7. 用数组来实现Stack

    1:Stack特点 stack:栈,是一种特殊的数据结构,有着先入后出的特点(first in last out).stack中栈底始终不变,只有一端能变化.栈顶在有数据push的时候增大,有数据po ...

  8. 12)hInstance和hWnd写进子类

    1)因为这些变量存在于 WInMaincpp文件中  但是  我想在我的CGameCtrl子类中要用到hInstance实例句柄和hWNd窗口句柄,那么 我就将这些变量在父类CGameCtrl中有一份 ...

  9. MySQL--MySQL 日志

    在 MySQL中,有 4 种不同的日志,分别是错误日志.二进制日志(BINLOG 日志).查询日志和慢查询日志. 1.错误日志 错误日志是 MySQL 中最重要的日志之一,它记录了当 mysqld 启 ...

  10. Python—守护进程管理工具(Supervisor)

    一.前言简介 1.Supervisor 是一个 Python 开发的 client/server 系统,可以管理和监控类 UNIX 操作系统上面的进程.可以很方便的用来启动.重启.关闭进程(不仅仅是 ...