forked from Python3WebSpider/Moments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
moments.py
119 lines (111 loc) · 4.02 KB
/
moments.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import os
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from pymongo import MongoClient
from time import sleep
from processor import Processor
from config import *
class Moments():
def __init__(self):
"""
初始化
"""
# 驱动配置
self.desired_caps = {
'platformName': PLATFORM,
'deviceName': DEVICE_NAME,
'appPackage': APP_PACKAGE,
'appActivity': APP_ACTIVITY
}
self.driver = webdriver.Remote(DRIVER_SERVER, self.desired_caps)
self.wait = WebDriverWait(self.driver, TIMEOUT)
self.client = MongoClient(MONGO_URL)
self.db = self.client[MONGO_DB]
self.collection = self.db[MONGO_COLLECTION]
# 处理器
self.processor = Processor()
def login(self):
"""
登录微信
:return:
"""
# 登录按钮
login = self.wait.until(EC.presence_of_element_located((By.ID, 'com.tencent.mm:id/cjk')))
login.click()
# 手机输入
phone = self.wait.until(EC.presence_of_element_located((By.ID, 'com.tencent.mm:id/h2')))
phone.set_text(USERNAME)
# 下一步
next = self.wait.until(EC.element_to_be_clickable((By.ID, 'com.tencent.mm:id/adj')))
next.click()
# 密码
password = self.wait.until(
EC.presence_of_element_located((By.XPATH, '//*[@resource-id="com.tencent.mm:id/h2"][1]')))
password.set_text(PASSWORD)
# 提交
submit = self.wait.until(EC.element_to_be_clickable((By.ID, 'com.tencent.mm:id/adj')))
submit.click()
def enter(self):
"""
进入朋友圈
:return:
"""
# 选项卡
tab = self.wait.until(
EC.presence_of_element_located((By.XPATH, '//*[@resource-id="com.tencent.mm:id/bw3"][3]')))
tab.click()
# 朋友圈
moments = self.wait.until(EC.presence_of_element_located((By.ID, 'com.tencent.mm:id/atz')))
moments.click()
def crawl(self):
"""
爬取
:return:
"""
while True:
# 当前页面显示的所有状态
items = self.wait.until(
EC.presence_of_all_elements_located(
(By.XPATH, '//*[@resource-id="com.tencent.mm:id/cve"]//android.widget.FrameLayout')))
# 上滑
self.driver.swipe(FLICK_START_X, FLICK_START_Y + FLICK_DISTANCE, FLICK_START_X, FLICK_START_Y)
# 遍历每条状态
for item in items:
try:
# 昵称
nickname = item.find_element_by_id('com.tencent.mm:id/aig').get_attribute('text')
# 正文
content = item.find_element_by_id('com.tencent.mm:id/cwm').get_attribute('text')
# 日期
date = item.find_element_by_id('com.tencent.mm:id/crh').get_attribute('text')
# 处理日期
date = self.processor.date(date)
print(nickname, content, date)
data = {
'nickname': nickname,
'content': content,
'date': date,
}
# 插入MongoDB
self.collection.update({'nickname': nickname, 'content': content}, {'$set': data}, True)
sleep(SCROLL_SLEEP_TIME)
except NoSuchElementException:
pass
def main(self):
"""
入口
:return:
"""
# 登录
self.login()
# 进入朋友圈
self.enter()
# 爬取
self.crawl()
if __name__ == '__main__':
moments = Moments()
moments.main()