其他分享
首页 > 其他分享> > 百度知道

百度知道

作者:互联网

环境搭建:

1、安装Microsoft.NET.Framework 4.5
2、安装node.js cmd下执行 node
3、安装appium
4、安装ADT工具 (直接在系统变量的path下面将platform-tools加进去) cmd下面执行 adb

5、安装安卓模拟器
6、安装 appium python
(pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn appium-python-Client)
7、需要开启手机 开发者模式及usb调试
8、安装包的包名后缀为apk
9、adb shell dumpsys activity |find “mFocusedActivity” 获取当前活跃前台包名

10、检测当前手机是否连接到电脑 adb devices

11.adt里面的sdk下的tool有uiautomator
12.adb shell dumpsys window w | findstr / | findstr name=

adb shell dumpsys window windows

强烈推荐!!!对应的目录下执行也能获取app的name
D:\android\SDK\build-tools\28.0.3> aapt dump badging d:\test.apk

import requests
from bs4 import BeautifulSoup
from jparser import PageModel


class baidup(object):

    def __init__(self,word):
        self.word = word

    def baidu(self):
        headers = {
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
            'Accept-Encoding': 'gzip, deflate, compress',
            'Accept-Language': 'en-us;q=0.5,en;q=0.3',
            'Cache-Control': 'max-age=0',
            'Connection': 'keep-alive',
            'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0'
        }

        url = 'http://www.baidu.com.cn/s?wd=' + self.word + '&pn=0'
        res = requests.get(url,headers=headers).text
        tagh3 = BeautifulSoup(res, 'lxml').find_all('h3')
        all_data = open('test.txt', 'w')
        i =0
        for h3 in tagh3:
            href = h3.find('a').get('href')
            baidu_url = requests.get(url=href, headers=headers, allow_redirects=False)
            real_url = baidu_url.headers['Location']  # 得到网页原始地址
            # print(real_url)
            if real_url.startswith('http'):
                all_data.write(real_url + '\n')
                response = requests.get(url=real_url, headers=headers)
                i=i+1
                if i>1:
                    break
                en_code = response.encoding
                de_code = response.apparent_encoding
                # print(en_code,de_code,'-----------------')
                if de_code == None:
                    if en_code in ['utf-8', 'UTF-8']:  # en_code=utf-8时,de_code=utf-8,可以获取到内容
                        de_code = 'utf-8'
                elif de_code in ['ISO-8859-1', 'ISO-8859-2', 'Windows-1254', 'UTF-8-SIG']:
                    de_code = 'utf-8'
                html = response.text.encode(en_code, errors='ignore').decode(de_code, errors='ignore')
                pm = PageModel(html)
                result = pm.extract()
                ans = [x['data'] for x in result['content'] if x['type'] == 'text']
                content = ''.join(ans)

                return content
# c= baidup("我")
# print(c.baidu())
from appium import webdriver
import time
import re
import string
from baidu.baidup import baidup
from selenium.webdriver.common.keys import Keys
#设备对象的信息准备
desired_caps = {}

#操作系统 -目标机
desired_caps["platformName"] = "Android"
#系统版本
desired_caps["platformVersion"] = "11"
#设备名称
desired_caps["deviceName"] = "db1a43ef"
#app信息
desired_caps["appPackage"] = "com.baidu.iknow"
#百度知道 com.baidu.iknow  百度的第一个页面 /com.baidu.iknow.activity.index.IndexActivity
#打开app的第一个页面
desired_caps["appActivity"] = "com.baidu.iknow.activity.common.PrologueActivity"
#不进行app重置信息
desired_caps["noReset"] = "true"

#连接appium server,并告诉其要操作的对象
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps)
time.sleep(5) #等3s的广告
#点击我的
driver.find_element_by_android_uiautomator('text(\"我的\")').click()
#点击在线聊
driver.find_element_by_android_uiautomator('text(\"在线聊\")').click()
#点击在线抢单可能没有单,需要try
try:
    time.sleep(2)
    driver.find_element_by_android_uiautomator('text(\"在线抢单\")').click()
    # print(driver.find_element_by_android_uiautomator('text(\"在线抢单\")').get_attribute("name"))
    time.sleep(3)
    driver.find_element_by_class_name("android.view.View").get_attribute("name")
    rest = driver.find_element_by_xpath('//*[contains(@text, "本平台禁止一切淫秽、暴力、攻击、违")]').get_attribute("name")
    # print(str(re.findall(r':\d+(.*)温', rest)).replace('[', '').replace(']', '').strip(string.punctuation))
    word = str(re.findall(r':\d+(.*)温', rest)).replace('[', '').replace(']', '').strip(string.punctuation)

    # print(driver.find_element_by_android_uiautomator('//*[contains(@text, "本平台禁止一切淫秽、暴力、攻击、违")]').get_attribute("text"))
except Exception as e:
    time.sleep(1)
    driver.find_element_by_android_uiautomator('text(\"我的\")').click()
    # 点击在线聊
    driver.find_element_by_android_uiautomator('text(\"在线聊\")').click()
    driver.find_element_by_android_uiautomator('text(\"在线抢单\")').click()
    time.sleep(3)
    print(driver.find_element_by_android_uiautomator('//*[contains(@text, "本平台禁止一切淫秽、暴力、攻击、违")]').get_attribute("text"))

# print("输出文本为"+a)
print(baidup(word).baidu())
time.sleep(2)
# driver.find_element_by_android_uiautomator('text(\"一起聊聊吧~\")').send_keys(baidup(word).baidu())
driver.find_element_by_android_uiautomator('text(\"一起聊聊吧~\")').click()
driver.find_element_by_android_uiautomator('text(\"一起聊聊吧~\")').click()
time.sleep(2)
driver.find_element_by_android_uiautomator('text(\"一起聊聊吧~\")').send_keys("祝你好运")

#
# driver.find_element_by_android_uiautomator('text(\"一起聊聊吧~\")').send_keys(Keys.ENTER)
time.sleep(2)
a1 = 971/ 1080
b1 = 1361 / 2120
# 获取当前手机屏幕大小X,Y
X = driver.get_window_size()['width']
Y = driver.get_window_size()['height']
# 屏幕坐标乘以系数即为用户要点击位置的具体坐标
driver.tap([(a1 * X, b1 * Y)])
# driver.find_element_by_xpath('//*[contains(@text, " 发送 ")]').click()
# driver.find_element_by_android_uiautomator('text(\" 发送 \")').click()
# driver.find_element_by_android_uiautomator('//*[contains(@text, "发送")]').click()
# driver.find_element_by_class_name("android.view.View").get_attribute("name")
# rest = driver.find_element_by_xpath('//*[contains(@text, "发")]').get_attribute("name")
# print(rest)

标签:uiautomator,text,driver,element,百度,android,find,知道
来源: https://blog.csdn.net/itlianjinshushi/article/details/111147543