一.豆瓣模拟登录(双层html)
#!/usr/bin/env python # -*- coding: utf-8 -*- #author tom import time from selenium import webdriver #构造浏览器,请求数据 bro=webdriver.Chrome() bro.get('https://www.douban.com/') time.sleep(2) #他的登录页面是双层嵌套的,要先转入里面那个页面,标签才能定位到 iframe=bro.find_element_by_tag_name('iframe') bro.switch_to_frame(iframe) #点击密码登录 pslogin=bro.find_elements_by_css_selector('body > div.account-body.login-wrap.login-start.account-anonymous > div.account-body-tabs > ul.tab-start > li.account-tab-account')[0] print(pslogin) print(type(pslogin)) pslogin.click() time.sleep(2) #填入密码 bro.find_element_by_id('username').send_keys('xxx') bro.find_element_by_id('password').send_keys('xxx') time.sleep(2) #点击登录 btn=bro.find_element_by_xpath('//a[@class="btn btn-account btn-active"]') btn.click()
cookies={i['name']:i['value']for i in bro.get_cookies()}
print(cookies)
time.sleep(3)
bro.quit(
二.人人模拟登录
''' 1.http://www.yundama.com/demo.html 2.注册普通用户和开发者用户 3.登录普通用户 查看余额 4.登录开发者用户:查看appid和key ''' import requests from lxml import etree from renrenlogin.Code import YDMHttp def get_code_img_text(userName,passWord,imgPath,imgType): result = None # 用户名 username = userName # 密码 password = passWord # 软件ID,开发者分成必要参数。登录开发者后台【我的软件】获得! appid = 7442 # 软件密钥,开发者分成必要参数。登录开发者后台【我的软件】获得! appkey = '1ef33b4c6cde16c03c510239c58506ed' # 图片文件 filename = imgPath # 验证码类型,# 例:1004表示4位字母数字,不同类型收费不同。请准确填写,否则影响识别率。在此查询所有类型 http://www.yundama.com/price.html codetype = imgType # 超时时间,秒 timeout = 20 # 检查 if (username == 'username'): print('请设置好相关参数再测试') else: # 初始化 yundama = YDMHttp(username, password, appid, appkey) # 登陆云打码 uid = yundama.login(); print('uid: %s' % uid) # 查询余额 balance = yundama.balance(); print('balance: %s' % balance) # 开始识别,图片路径,验证码类型ID,超时时间(秒),识别结果 cid, result = yundama.decode(filename, codetype, timeout); print('cid: %s, result: %s' % (cid, result)) return result headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36' }
#实现携带验证码的模拟登录 #1.获取验证码图片 url = 'http://www.renren.com/' page_text = requests.get(url=url,headers=headers).text #2.解析出验证码图片 tree = etree.HTML(page_text) code_img_url = tree.xpath('//*[@id="verifyPic_login"]/@src')[0] #将验证码图片存储到本地 code_img_data = requests.get(url=code_img_url,headers=headers).content with open('./code.jpg','wb') as fp: fp.write(code_img_data) #识别验证码图片中的数据值 code_text = get_code_img_text('自己普通用户','自己的普通密码','./code.jpg',2004) # print(code_text) #模拟登录 url = 'http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=201934120373'
#data的数据可以通过fiddle抓包工具,在浏览器登录的试试抓包获得 data={ 'captcha_type':'web_login', 'domain':'renren.com', 'email':'302624476@qq.com', 'f':'http%3A%2F%2Fwww.renren.com%2F323264874%2Fprofile', 'icode':code_text, 'key_id':'1', 'origURL':'http://www.renren.com/home', 'password':'xxxxxx', 'rkey':'6d7d2086ddd44c39ae683b6d93c0a39f' } #创建一个session:session也可以发送请求.session可以自动保存cookie session = requests.session() #使用session进行的请求发送,如果请求成功后会产生cookie,则该cookie会被自动存储在该session对象中 session.post(url=url,data=data,headers=headers) #携带cookie对个人主页的url发起请求 pesonal_url = 'http://www.renren.com/323264874/profile' person_page_text = session.get(url=pesonal_url,headers=headers).text with open('./renren.html','w',encoding='utf-8') as fp: fp.write(person_page_text) print('over!!!')
code:
''' 1.http://www.yundama.com/demo.html 2.注册:普通用户和开发者用户 3.登录操作: 3.1 普通用户:查看余额. 3.2 开发者用户: - 创建一个软件: - 下载示例程序:开发文档->下载新版本的DLL->pythonHTTP ''' import http.client, mimetypes, urllib, json, time, requests ###################################################################### class YDMHttp: apiurl = 'http://api.yundama.com/api.php' username = '' password = '' appid = '' appkey = '' def __init__(self, username, password, appid, appkey): self.username = username self.password = password self.appid = str(appid) self.appkey = appkey def request(self, fields, files=[]): response = self.post_url(self.apiurl, fields, files) response = json.loads(response) return response def balance(self): data = {'method': 'balance', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey} response = self.request(data) if (response): if (response['ret'] and response['ret'] < 0): return response['ret'] else: return response['balance'] else: return -9001 def login(self): data = {'method': 'login', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey} response = self.request(data) if (response): if (response['ret'] and response['ret'] < 0): return response['ret'] else: return response['uid'] else: return -9001 def upload(self, filename, codetype, timeout): data = {'method': 'upload', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey, 'codetype': str(codetype), 'timeout': str(timeout)} file = {'file': filename} response = self.request(data, file) if (response): if (response['ret'] and response['ret'] < 0): return response['ret'] else: return response['cid'] else: return -9001 def result(self, cid): data = {'method': 'result', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey, 'cid': str(cid)} response = self.request(data) return response and response['text'] or '' def decode(self, filename, codetype, timeout): cid = self.upload(filename, codetype, timeout) if (cid > 0): for i in range(0, timeout): result = self.result(cid) if (result != ''): return cid, result else: time.sleep(1) return -3003, '' else: return cid, '' def report(self, cid): data = {'method': 'report', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey, 'cid': str(cid), 'flag': '0'} response = self.request(data) if (response): return response['ret'] else: return -9001 def post_url(self, url, fields, files=[]): for key in files: files[key] = open(files[key], 'rb'); res = requests.post(url, files=files, data=fields) return res.text
三.关于验证码识别登录
####验证码识别
- 验证码图片url不变,验证码不变
- 请求验证码图片获得验证码图片, 通过打码品台识别
- 验证码url不变,验证码会变
- 思路:对方服务器在返回烟验证码的时候,会和每个用户信息进行一个对应,之后再用户发起post请求的时候,会对比post请求的中的验证码和当前用户真正存储在服务器端的验证码是否相同
- 操作:
- 实例化session
- 使用session请求登录页面获得验证的地址
- 使用session请求验证码,识别
- 使用session发送post请求
- 使用selenniu登录,遇到验证码
- url不变,验证码不变,同上
- url不变,验证码会变
- 使用selenium请求登录页面,同时拿到验证码地址
- 获取登录页面中的cookie,就是实例化的浏览器中使用get——cookies,交给request模块去请求,识别
- 输入验证码识别