有偿!利用appium爬取微信朋友圈数据出现问题!
以下代码陷入了死循环,不知道该怎么解决,爬取的信息也没办法正常保存,求大佬解决,有偿!!!!1 def get_data(self):
while True:
# 获取 ListView
items = self.wait.until(EC.presence_of_all_elements_located((By.ID, 'com.tencent.mm:id/fol')))
#presence_of_all_elements_located:判断是否至少有1个元素存在于dom树中。
# 滑动
self.driver.swipe(self.start_x, self.start_y, self.end_x, self.end_y, 1000)
# 遍历获取每个List数据
for item in items:
try:
# moment_text = item.find_element_by_id('com.tencent.mm:id/b3b').text
# 日期
day_text = item.find_element_by_id('com.tencent.mm:id/fnr').get_attribute('text')
# 月份
month_text = item.find_element_by_id('com.tencent.mm:id/fmo').get_attribute('text')
# 昵称
nickname = item.find_element_by_id('com.tencent.mm:id/e3x').get_attribute('text')
# 正文
if item.find_element_by_id('com.tencent.mm:id/b_l').get_attribute('text'):
content = item.find_element_by_id('com.tencent.mm:id/b_l').get_attribute('text')
else:
content = item.find_element_by_id('com.tencent.mm:id/b_').get_attribute('text')
if content == '':
pass
else:
print(month_text+day_text,nickname + ':', content)
f = open('contents.txt', 'a', encoding='utf-8')
f.write('%s' % month_text + '%s' % day_text+'%s' % nickname + ':' + '%s' % content)
f.write( '\n' + '------------------------------------' + '\n')
time.sleep(3)
except NoSuchElementException:
pass