栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 后端开发 > Python

appium毒——球鞋怎么买?

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

appium毒——球鞋怎么买?

#-*- coding:utf-8 -*-
from appium import webdriver
from selenium.webdriver.common.by import By
from appium.webdriver.extensions.android.nativekey import AndroidKey
import time
import csv

desired_caps = {
  'platformName': 'Android', # 被测手机是安卓
  'platformVersion': '7', # 手机安卓版本
  'deviceName': 'xxx', # 设备名,安卓手机可以随意填写
  'appPackage': 'com.shizhuang.duapp', # 启动APP Package名称
  'appActivity': '.modules.home.ui.SplashActivity', # 启动Activity名称
  'unicodeKeyboard': True, # 使用自带输入法,输入中文时填True
  'resetKeyboard': True, # 执行完程序恢复原来输入法
  'noReset': True,       # 不要重置App
  'newCommandTimeout': 6000,
  'automationName' : 'UiAutomator2'
  # 'app': r'd:apkbili.apk',
}

# 连接Appium Server,初始化自动化环境
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

# 设置缺省等待时间
driver.implicitly_wait(10)

driver.find_element(By.ID, 'ivSearch').click()
time.sleep(2)
# # 根据id定位搜索位置框,点击
# driver.find_element(By.ID, 'collapseSearchArea').click()
# time.sleep(2)
# 根据id定位搜索输入框,点击
sbox = driver.find_element(By.ID, 'etSearch')
sbox.send_keys('篮球鞋')
time.sleep(2)
driver.find_element(By.ID, 'tvSearch').click()
time.sleep(2)
driver.find_element(By.XPATH, '//android.widget.TextView[@text="商品"]').click()
time.sleep(2)
driver.find_element(By.XPATH, '//android.widget.TextView[@text="筛选"]').click()
time.sleep(2)
driver.find_element(By.XPATH, '//android.widget.TextView[@text="0-349"]').click()
time.sleep(2)
driver.find_element(By.ID, 'tvConfirm').click()
time.sleep(2)
driver.find_element(By.XPATH, '//android.widget.TextView[@text="累计销量"]').click()
time.sleep(2)

# # 每翻页就不一样? 会有重复值吗?
# search_titles = driver.find_elements(By.ID, 'search_title')
#
# for search_title in search_titles:
#     # 打印标题
#     print(search_title.text)



def save_data():
    # tree = etree.HTML(html_str)
    # li_list = driver.find_elements(By.ID, 'search_root') #所有tr
    search_title = driver.find_elements(By.ID, 'itemTitle')
    # print()
    # for li in li_list:#这玩意叫列表遍历
    # search_image = driver.find_elements(By.ID, 'search_title')[0]
    for i in range(0,len(search_title)):

        try:
            search_title = driver.find_elements(By.ID, 'itemTitle')[i]
            # print(search_title.text)
            search_price = driver.find_elements(By.ID, 'itemPrice')[i]
            # print(search_price.text)
            search_sold= driver.find_elements(By.ID, 'itemSoldNum')[i]
            # print(search_sold.text)
        except IndexError:
            pass
        item = {}
        item['search_title'] = search_title.text
        item['search_price'] = search_price.text
        item['search_sold'] = search_sold.text

        datas = []
        dic = item
        # print(item)
        fieldnames = ['search_title', 'search_price', 'search_sold']
        datas.append(dic)
        with open('5.csv', 'a', newline='', encoding='utf-8-sig') as f:
            writer = csv.DictWriter(f, fieldnames=fieldnames,dialect="excel")  # 提前预览列名,当下面代码写入数据时,会将其一一对应。
            writer.writeheader()  # 写入列名
            writer.writerows(datas)  # 写入数据


def getSize():                               #获取当前的width和height的x、y的值
    x = driver.get_window_size()['width']   #width为x坐标
    y = driver.get_window_size()['height']  #height为y坐标
    # print(x,y)
    return (x, y)

def swipeUp(t):  #当前向上滑动swipeup
    l = getSize()
    x1 = int(l[0] * 0.5)
    y1 = int(l[1] * 0.75)
    y2 = int(l[1] * 0.25)
    driver.swipe(x1, y1, x1, y2,500)  #设置时间为500
# swipeUp(9000)     #向上滑动9000

def swipLeft(t):    #当前向左进行滑动swipleft
    l=getSize()
    x1=int(l[0]*0.75)
    y1=int(l[1]*0.5)
    x2=int(l[0]*0.05)
    driver.swipe(x1,y1,x2,y1,500)
# swipLeft(3000)        #向左滑行3000

def swipeDown(t):    #向下滑动swipedown
    l = getSize()
    print(l)
    x1 = int(l[0] * 0.5)
    y1 = int(l[1] * 0.25)
    y2 = int(l[1] * 0.75)
    driver.swipe(x1, y1, x1, y2,500)
    print(x1, y1, y2)
# swipeDown(1000)   #向下滑动10000
# time.sleep(2)
# swipeDown(1000)   #向下滑动10000
# time.sleep(2)
# swipeDown(1000)   #向下滑动10000

def swipRight(t): #向右滑行swipright
    l=getSize()
    x1=int(l[0]*0.05)
    y1=int(l[1]*0.5)
    x2=int(l[0]*0.75)
    driver.swipe(x1,y1,x2,y1,500)
# swipRight(3000)    #向右滑行3000,回到初始位置

for i in range(0,1000):
    save_data()
    time.sleep(2)
    swipeUp(960)
    print('第%s页爬取完成'%i)

转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/1038002.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号