前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >终极利器!利用appium和mitmproxy登录获取cookies

终极利器!利用appium和mitmproxy登录获取cookies

作者头像
佛系编程人
发布2019-08-14 15:14:38
2.1K0
发布2019-08-14 15:14:38
举报
文章被收录于专栏:佛系编程人佛系编程人

环境搭建

参考我之前写的windows中Appium-desktop配合夜神模拟器的使用

appium

代码start_appium.py

代码语言:javascript
复制
# -*- coding: utf-8 -*-
# @Time    : 2018/10/8 11:00
# @Author  : cxa
# @File    : start_appium.py
# @Software: PyCharmctx
from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
import base64


def start_appium():
    desired_caps = {}
    desired_caps['platformName'] = 'Android'  # 设备系统
    desired_caps['deviceName'] = '127.0.0.1:62001'  # 设备名称
    desired_caps['appPackage'] = 'com.xxxx.xxxx'  # 测试app包名,如何获取包名方式看上面的环境搭建。
    desired_caps['appActivity'] = 'com.xxxx.xxxx.xxx.xxxx'  # 测试appActivity,如何获取包名方式看上面的环境搭建。
    desired_caps['platformVersion'] = '4.4.2'  # 设备系统的安卓版本,版本不要太高,设计安全策略得外部因素。
    desired_caps['noReset'] = True  # 启动后结束后不清空应用数据
    desired_caps['unicodeKeyboard'] = True  # 此两行是为了解决字符输入不正确的问题
    desired_caps['resetKeyboard'] = True  # 运行完成后重置软键盘的状态  
    driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)  # 启动app,启动前记得打开appium服务。
    wait = WebDriverWait(driver, 60)#设置等待事件
    try:
        btn_xpath = '//android.widget.Button[@resource-id="com.alicom.smartdail:id/m_nonum_confirm_btn"]'
        btn_node = wait.until(EC.presence_of_element_located((By.XPATH, btn_xpath)))#等元素出现再继续,最长等待时间上面设置的60s。
        # btn_node=driver.find_element_by_xpath(btn_xpath)
        btn_node.click()
    except:
        driver.back()
        btn_xpath = '//android.widget.Button[@resource-id="com.alicom.smartdail:id/m_nonum_confirm_btn"]'
        btn_node = wait.until(EC.presence_of_element_located((By.XPATH, btn_xpath)))
        # btn_node = driver.find_element_by_xpath(btn_xpath)
        btn_node.click()
    # sleep 30s
    # 点击


def login_in(driver):
    id_xpath = '//android.widget.EditText[@content-desc="账户名输入框"]'
    id_node = driver.find_element_by_xpath(id_xpath)
    id_node.clear()
    id_node.send_keys("test")
    pwd = str(base64.b64decode("MTIzNHF3ZXI="), 'u8')
    pwd_xpath = '//android.widget.EditText[@content-desc="密码输入框"]'
    pwd_node = driver.find_element_by_xpath(pwd_xpath)
    pwd_node.clear()
    pwd_node.send_keys(pwd)
    submit = "//android.widget.Button[@text='登录']"
    submit_node = driver.find_element_by_xpath(submit)
    submit_node.click()
    time.sleep(10)


if __name__ == '__main__':
    start_appium()

mitmproxy

代码 mitm_proxy_script.py

代码语言:javascript
复制
# -*- coding: utf-8 -*-
# @Time    : 2018/10/8 11:00
# @Author  : cxa
# @File    : mitm_proxy_script.py
# @Software: PyCharm
import sys

def request(flow):
    request = flow.request
    if '.png' in request.url or 'xxx.x.xxx.com' not in request.url:
        return  #如果不在观察的url内则返回
    if 'xxx.x.xxx.com' in request .url:
        print(request .url)
        cookies = dict(request.cookies) #转换cookies格式为dict
        if cookies:
            save_cookies(repr(cookies))#如果不为空保存cookies


def save_cookies(cookies):
    sys.path.append("../")
    from database import getcookies
    getcookies.insert_data(sitename, cookies) #保存cookies

后续还将进一步完善,请点击阅读原文根据状态。

本文章作者陈哥是高级爬虫师哦~他的微信群活跃都很高,光是看看就能学习到很多新的有用的知识。如果你想往爬虫这个方向前进,可以加陈哥的微信号:italocxa

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-05-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 佛系编程人 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 环境搭建
  • appium
  • mitmproxy
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档