Python爬虫带用户名密码登录
# -*- 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爬虫带用户名密码登录的更多相关文章
- Python用户名密码登录系统(MD5加密并存入文件,三次输入错误将被锁定)及对字符串进行凯撒密码加解密操作
# -*- coding: gb2312 -*- #用户名密码登录系统(MD5加密并存入文件)及对字符串进行凯撒密码加解密操作 #作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.co ...
- python爬虫scrapy框架——人工识别登录知乎倒立文字验证码和数字英文验证码(2)
操作环境:python3 在上一文中python爬虫scrapy框架--人工识别知乎登录知乎倒立文字验证码和数字英文验证码(1)我们已经介绍了用Requests库来登录知乎,本文如果看不懂可以先看之前 ...
- git clone 带用户名密码的形式但包含@等特殊符号无法正常解析
正常使用git clone 的方式 git clone https://remote 使用带用户名密码的方式(可以避免后续每次都要输入用户名密码) git clone https://[usernam ...
- pyhton学习,day1作业,用户名密码登录模块
要求,通过用户名密码登录,登录错误3次,锁定用户名 # coding=utf-8 # Author: RyAn Bi import os, sys #调用系统自己的库 accounts_file = ...
- 【Python练习】文件引用用户名密码登录系统
一.通过txt文件引入用户名密码 1 #coding=utf-8 from selenium import webdriver #from selenium.common.exceptions imp ...
- [Python爬虫] Selenium实现自动登录163邮箱和Locating Elements介绍
前三篇文章介绍了安装过程和通过Selenium实现访问Firefox浏览器并自动搜索"Eastmount"关键字及截图的功能.而这篇文章主要简单介绍如何实现自动登录163邮箱,同时 ...
- Python实现LDAP用户名密码验证
网上借鉴了不少东西,下面是python代码,备份后用. 思路,因为每个用户的组都不一样,这样就导致了dn不一致的情况, 据需要先根据用户名获取该用户的dn,然后再bind用户名和密码进行验证. 反正是 ...
- 用户名密码登录小程序及input与raw_input区别。
一.此次程序需要实现: 1.设定固定的用户名密码 2.用户名密码输入正确打印登录正确信息 3.仅仅运行三次登录 二.本次使用的python版本为: Windows下版本号: C:\Users\dais ...
- Python爬虫之用脚本登录Github并查看信息
前言分析目标网站的登录方式 目标地址:https://github.com/login 登录方式做出分析: 第一,用form表单方式提交信息, 第二,有csrf_token, 第三 ,是以po ...
随机推荐
- linux下创建swap分区
两种不同的方式创建swap分区 第一种方法: fdisk /dev/sda n (新建一个分区为/dev/sda6) t (修改分区的id) 82 (swap的id为82) w (重写分区表) par ...
- Thread.sleep 与Thread.currentThread.sleep 相同
package com.citi.tm.api.trade.mongo; public class ThreadTest { public static void main(String[] args ...
- 浅copy
person=['aaa',['a',bbb'] p1=copy.copy(person) p2=person[:] p3=list(person) p4=person.copy() print(ty ...
- android 根据距离区分 点击跟滑动事件
public void onClick(View v) { if (isclick) Log.i(TAG, "onclick"); } }); } float distance = ...
- 17. docker 网络 host 和 none
1.none network 创建一个 none 网络的 container test1 docker run --name test1 --network none busybox /bin/sh ...
- R语言 批量下载财务报表
getsheets <- function(symbol,type,file){ pre="http://money.finance.sina.com.cn/corp/go.php/v ...
- windows下CreateDirectory创建路径失败的解决办法
第一: 权限不够: SECURITY_ATTRIBUTES sa;SECURITY_DESCRIPTOR sd; InitializeSecurityDescriptor(&sd,SECURI ...
- Graph & Trees3 - 二分图
\[二分图略解\] \[By\;TYQ\] 二分图定义: \(f(i,L) = [a \in L\;\text{&}\;\forall b \in a.to \;\text{,}\; b \n ...
- 获取文件MD5值(JS、JAVA)
文章HTML代码翻译于地址:https://www.cnblogs.com/linyihai/p/7040786.html 文件MD5有啥用? 文 ...
- vue 中使用print.js 打印遇到的问题 ?
不管怎么设置打印部分的 margin和height 仍会在预览时多出一张空白页?求各位大佬遇到过的请留言谢谢!