# -*- 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. 微信H5支付,成功样例

    <?php/** * Created by PhpStorm. * User: Administrator * Date: 2019/6/3 * Time: 12:00 */ if( !defi ...

  2. Node.js—小试牛刀-创建目录

    今天第一天学习Node.js  感觉特别亲切 //文件结构 //引入模块 const path=require('path') const fs=require('fs') let roots='H: ...

  3. Linux-常见信号介绍

    1.SIGINT           2 Ctrl + C时OS送给前台进程组中每个进程 2.SIGABRT             6              调用abort函数,进程异常终止 3 ...

  4. Java面向对象(概述,构造函数,类与对象的关系,this关键字,成员、局部),匿名对象的调用,构造代码块(5)

    Java面向对象(概述,构造函数,类与对象的关系,this关键字,成员.局部),匿名对象的帝爱用,构造代码块(5)

  5. This inspection highlights chained comparisons that can be simplified.

    https://stackoverflow.com/questions/26502775/pycharm-simplify-chained-comparison In Python you can & ...

  6. JNI的第1种写法 及 JNI通过形参修改Java数据

    声明:迁移自本人CSDN博客https://blog.csdn.net/u013365635 目的:通过形参在Java 和C之间传递数据,尤其是大块的媒体数据 优点:避免通过返回值返回一个巨大的数据块 ...

  7. 第3章 ZooKeeper基本数据模型

    第3章 ZooKeeper基本数据模型 3-1 zk数据模型介绍 3-2 zk客户端连接关闭服务端,查看znode ./zkCli.sh Ctrl + C 退出 =================== ...

  8. UML-为什么要画领域模型?

    不熟悉业务的情况下, 1).找到关键概念和词汇 2).概念间的关系 熟悉业务的情况下, 1).梳理思路 完美不是目的. 另外,减少标示差异.如下图

  9. android 设置无标题栏主题

    <application android:theme="@style/Theme.AppCompat.Light.NoActionBar">

  10. Python入门方法推荐,哪些基础知识必学?

    很多想入门的小伙伴还不知道Python应该怎么学,哪些知识必学,今天我们就来盘点一下. 01.入门方法推荐 总体来讲,找一本靠谱的书,由浅入深,边看边练. 网上的学习教程有很多,多到不知道如何选择.所 ...