关于time包出现的问题

来源:-10 短视频网站appium模拟滑动操作2

和尚不挑水

2019-04-13

老师,我在写完粉丝列表自动滑动的代码后,打开appium测试,前几步都正常运行,运行到粉丝列表的时候,本该进行自动滑动操作,但是没有出现这一操作,并且报了图一所示的错误,在百度上查了之后,说是from datetime import time中的time与time.sleep(1)中的time冲突了,于是在上边又加上了import time,这次到粉丝列表时进行了自动滑动操作,但是并没有实际的向上滑,只是在屏幕上出现了滑动的操作而已,并且报了如图二所示的错误,请问是导包的错误吗还是别的错误。
#appium

from datetime import time
from appium import webdriver#用来等待元素控件
from selenium.webdriver.support.ui import WebDriverWait

cap = {"platformName": "Android",
       "platformVersion": "4.4.2",
       "deviceName": "127.0.0.1:62025",
       "appPackage": "com.ss.android.ugc.aweme",
       "appActivity": "com.ss.android.ugc.aweme.splash.SplashActivity",
       "noReset": True,
       "unicodekeyboard": True,
       "resetkeyboard": True}

driver = webdriver.Remote("http://localhost:4723/wd/hub",cap)

#获取尺寸的函数
def get_size():
    x = driver.get_window_size()['width']
    y = driver.get_window_size()['height']
    return(x,y)

#点击搜索
try:
    if WebDriverWait(driver,10).until(lambda x:x.find_element_by_id("com.ss.android.ugc.aweme:id/aos")):
        driver.find_element_by_id("com.ss.android.ugc.aweme:id/aos").click()
except:
    pass
#定位的是搜索框
if WebDriverWait(driver,10).until(lambda x:x.find_element_by_xpath("//android.widget.EditText[@resource-id=‘com.ss.android.ugc.aweme:id/aeq’]")):
    driver.find_element_by_xpath("//android.widget.EditText[@resource-id=‘com.ss.android.ugc.aweme:id/aeq’]").click()
    driver.find_element_by_xpath("//android.widget.EditText[@resource-id=‘com.ss.android.ugc.aweme:id/aeq’]").send_keys("191433445")
    while driver.find_element_by_xpath("//android.widget.EditText[@resource-id=‘com.ss.android.ugc.aweme:id/aeq’]").text != "191433445":
        driver.find_element_by_xpath("//android.widget.EditText[@resource-id=‘com.ss.android.ugc.aweme:id/aeq’]").send_keys("191433445")
        time.sleep(0.1)
#搜索
driver.find_element_by_xpath("//android.widget.TextView[@resource-id=‘com.ss.android.ugc.aweme:id/aet’]").click()
#点击用户标签
if WebDriverWait(driver,10).until(lambda x:x.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.HorizontalScrollView/android.widget.LinearLayout/android.support.v7.app.ActionBar.Tab[3]/android.widget.LinearLayout/android.widget.TextView")):
    driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.HorizontalScrollView/android.widget.LinearLayout/android.support.v7.app.ActionBar.Tab[3]/android.widget.LinearLayout/android.widget.TextView").click()
#查看是否有关注标签
if WebDriverWait(driver,10).until(lambda x:x.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.support.v4.view.ViewPager/android.widget.FrameLayout/android.widget.FrameLayout[2]/android.view.View/android.support.v7.widget.RecyclerView/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[2]/android.widget.RelativeLayout/android.widget.TextView")):
    driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.support.v4.view.ViewPager/android.widget.FrameLayout/android.widget.FrameLayout[2]/android.view.View/android.support.v7.widget.RecyclerView/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[1]/android.widget.ImageView[2]").click()
#查看是否有粉丝
if WebDriverWait(driver,10).until(lambda x:x.find_element_by_xpath("//android.widget.TextView[@text=‘粉丝’]")):
    driver.find_element_by_xpath("//android.widget.TextView[@text=‘粉丝’]").click()
    time.sleep(1)     #就是这步提示出错!!!

l = get_size()
x1 = int(l[0]*0.5)
y1 = int(l[1]*0.9)
y2 = int(l[1]*0.15)
while True:
    if "暂时没有更多了" in driver.page_source:
        break
    else:
        #初始鼠标位置,从哪开始,结束时鼠标位置,到哪结束
        driver.swipe(x1,y1,x1,y2)
        time.sleep(0.1)
写回答

1回答

大壮老师

2019-04-13

由于抖音app升级等原因,建议使用如下方法抓取抖音数据,该方法预计下个月吧视频更新到本课程中。代码参照:

#email:dazhuang_python@sina.com
#time:20190414

import uiautomator2 as u2
import time


#使用uiautomator2抓取抖音粉丝数据
#1、uiautomator2模块的安装
    # pip install --upgrade --pre uiautomator2
    # pip install pillow
#2、weditor模块的安装,作用同uiautomator viewer
    #pip install -U weditor
#3、初始化手机agent
    #python -m uiautomator2 init

class Handle_douyin(object):
    #初始化设备
    def __init__(self):
        #连接手机
        self.d = u2.connect("MBC6OBPJSSG6IJSW")
        #启动抖音app
        self.d.app_start("com.ss.android.ugc.aweme")

    #获取手机显示尺寸
    def get_size(self):
        size = self.d.window_size()
        return size

    #滑动逻辑
    def handle_swipe(self):
        #点击搜索
        if self.d(resourceId="com.ss.android.ugc.aweme:id/aft").exists(timeout=10):
            self.d(resourceId="com.ss.android.ugc.aweme:id/aft").click()
        #定位到搜索框
        if self.d(resourceId="com.ss.android.ugc.aweme:id/jt").exists(timeout=10):
            self.d(resourceId="com.ss.android.ugc.aweme:id/jt").click()
            time.sleep(1)
        #发送要搜索的id
        self.d(resourceId="com.ss.android.ugc.aweme:id/jt").set_text("191433445")
        #点击搜索
        self.d(resourceId="com.ss.android.ugc.aweme:id/a8w").click()
        #点击用户标签
        if self.d(resourceId="android:id/text1", text=u"用户").exists(timeout=10):
            self.d(resourceId="android:id/text1", text=u"用户").click()
            time.sleep(1)
        #点击明星头像,使用坐标点击
        self.d.click(0.104, 0.211)
        #点击粉丝标签
        if self.d(text=u"粉丝").exists(timeout=10):
            self.d(text=u"粉丝").click()

        l = self.get_size()
        x1 = int(l[0] * 0.5)
        y1 = int(l[1] * 0.9)
        y2 = int(l[1] * 0.15)
        while True:
            self.d.swipe(x1, y1, x1, y2)
            time.sleep(0.2)

if __name__ == '__main__':
    douyin = Handle_douyin()
    douyin.handle_swipe()

weditor启动:

python -m weditor

启动后会启动浏览器,并打开

http://localhost:17310/#

获取app元素参照如下方法:

//img.mukewang.com/szimg/5cb33c7b0001ad3f19130848.jpg


另外在你的代码中time要使用

import time
不要使用
from datetime import time

关于uiautomator2相关知识点可以参照:

https://github.com/openatx/uiautomator2

或在QQ群以及本课程提问区与我讨论。

2
13
鲁智深110
回复
大壮老师
看到了,感觉一盘大餐,饥渴
2020-05-26
共13条回复

移动端Python爬虫实战 数据抓取+数据可视化

从App数据抓取到数据可视化,全流程讲解

1844 学习 · 1006 问题

查看课程