環(huán)境安裝教程
coding:utf-8
import time
from appium import webdriver
from pymongo import MongoClient
import threading
# 鏈接數(shù)據(jù)庫(kù)
con = MongoClient('127.0.0.1', 27017)
db = con.xiaohongshu
cur = db.xhs
caps = {
"platformName": "Android",
"deviceName": "127.0.0.1:62025",
"appPackage": "com.xingin.xhs",
"platformVersion": "5.1.1",
"appActivity": ".activity.SplashActivity",
"noReset": True, # 免登陸TRUE
"unicodeKeyboard": True # 解決不能輸入中文的問(wèn)題
}
獲得屏幕尺寸數(shù)據(jù)
def getSize():
x = driver.get_window_size()['width']
y = driver.get_window_size()['height']
return (x, y)
屏幕向下滑動(dòng)
def swipeUp():
l = getSize()
x1 = int(l[0] * 0.5) # x坐標(biāo)
y1 = int(l[1] * 0.75) # 起始y坐標(biāo)
y2 = int(l[1] * 0.25) # 終點(diǎn)y坐標(biāo)
driver.swipe(x1, y1, x1, y2)
屏幕向上滑動(dòng)
def swipeDown():
l = getSize()
x1 = int(l[0] * 0.5) # x坐標(biāo)
y1 = int(l[1] * 0.25) # 起始y坐標(biāo)
y2 = int(l[1] * 0.75) # 終點(diǎn)y坐標(biāo)
driver.swipe(x1, y1, x1, y2)
獲取想要的數(shù)據(jù)
def data():
title = driver.find_element_by_id("com.xingin.xhs:id/bdb").text
content = driver.find_element_by_id("com.xingin.xhs:id/bbo").text
print("標(biāo)題------>", title)
print("內(nèi)容------>", content)
swipeUp()
# swipeUp(3500)
try:
collect = driver.find_element_by_id("com.xingin.xhs:id/bcc").text
except Exception as e:
print(e)
collect = '無(wú)收藏'
try:
like_num = driver.find_element_by_id("com.xingin.xhs:id/bd3").text
except Exception as e:
print(e)
like_num = '無(wú)點(diǎn)贊'
try:
comments = driver.find_elements_by_id("com.xingin.xhs:id/bbo")[1].text
except Exception as e:
print(e)
comments = '無(wú)評(píng)論'
data = {'title': title, 'content': content, 'comments': comments, 'collect': collect, 'like_num': like_num}
print('存入數(shù)據(jù)庫(kù)前乒躺。。。噪矛。余蟹。作郭。诗充。溺健。吹艇。惰蜜。。受神。')
cur.insert(data)
print('存入成功抛猖。。鼻听。财著。。撑碴。撑教。。醉拓。伟姐。')
if name == 'main':
# 鏈接app
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', caps)
time.sleep(2)
# 首頁(yè)輸入框
t = driver.find_element_by_id("com.xingin.xhs:id/we").click()
time.sleep(2)
# 真實(shí)輸入框
text = driver.find_element_by_id("com.xingin.xhs:id/aiw")
time.sleep(2)
text.send_keys(u'巴黎歐萊雅')
time.sleep(2)
# 搜索按鍵
driver.find_element_by_id("com.xingin.xhs:id/aiz").click()
time.sleep(2)
# 向下滑動(dòng),刷新數(shù)據(jù)
swipeDown()
# swipeDown(500)
time.sleep(2)
while True:
print("begin======-------=======")
try:
# 點(diǎn)擊進(jìn)入詳情頁(yè)面
driver.find_element_by_id('com.xingin.xhs:id/ahd').click()
time.sleep(3)
data()
swipeDown()
# swipeDown(1000)
time.sleep(2)
# 返回上一頁(yè)
driver.find_element_by_class_name('android.widget.ImageButton').click()
# time.sleep(2)
swipeUp()
# swipeUp(200)
# 測(cè)試第二次進(jìn)入
driver.find_element_by_id('com.xingin.xhs:id/ahd').click()
time.sleep(3)
data()
swipeDown()
# swipeDown(1000)
# 返回上一頁(yè)
driver.find_element_by_class_name('android.widget.ImageButton').click()
swipeUp()
# swipeUp(200)
# time.sleep(2)
except:
driver.find_element_by_id('com.xingin.xhs:id/a4g').click()
time.sleep(2)
swipeUp()
# swipeUp(350)