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 ...
随机推荐
- opencv matchTemplate函数用法
模板匹配函数,就是在一幅图中,找到另外一幅的在本图的相似的地方 CV_EXPORTS_W void matchTemplate( InputArray image, InputArray templ, ...
- 超级顽固的流方式读取doc,docx乱码问题
因为工作中需要一个把doc或者docx的office文档内容,需要读取出来,并且也没展示功能.代码中第一考虑可能就是通过读取流方式,结果写了以后,各种乱码,百科的解决方案也是千奇百怪,第一点:可能是文 ...
- 刷题33. Search in Rotated Sorted Array
一.题目说明 这个题目是33. Search in Rotated Sorted Array,说的是在一个"扭转"的有序列表中,查找一个元素,时间复杂度O(logn). 二.我的解 ...
- LeetCode——787. K 站中转内最便宜的航班
有 n 个城市通过 m 个航班连接.每个航班都从城市 u 开始,以价格 w 抵达 v. 现在给定所有的城市和航班,以及出发城市 src 和目的地 dst,你的任务是找到从 src 到 dst 最多经过 ...
- Struts 2的流程
Struts 2的流程 一.Struts 2 的开发步骤: 在web.xml中定义核心的Filter来拦截用户的请求. 由于Web应用时基于请求/响应架构的应用,所以不管哪个MVC Web框架,都需要 ...
- Django专题-Cookie
Cookie Cookie的由来 大家都知道HTTP协议是无状态的. 无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应情况直接影响, ...
- 1017A.The Rank#排名
题目出处:http://codeforces.com/problemset/problem/1017/A #include<iostream> using namespace std; i ...
- Tooltips2
#include<windows.h> #include<Commctrl.h> #include"resource.h" #pragma comment( ...
- 网页滚动条CSS样式
滚动条样式主要涉及到如下CSS属性: overflow属性: 检索或设置当对象的内容超过其指定高度及宽度时如何显示内容 overflow: auto; 在需要时内容会自动添加滚动条overflow: ...
- POJ-2031 Building a Space Station (球的最小生成树)
http://poj.org/problem?id=2031 Description You are a member of the space station engineering team, a ...