标题:有偿!利用appium爬取微信朋友圈数据出现问题!
只看楼主
夜莺不会唱歌
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2020-10-7
结帖率:0
已结贴  问题点数:10 回复次数:2 
有偿!利用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
搜索更多相关主题的帖子: text content com item 数据 
2020-10-07 15:05
fall_bernana
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:17
帖 子:240
专家分:2086
注 册:2019-8-16
得分:10 
以下是引用夜莺不会唱歌在2020-10-7 15:05:24的发言:

以下代码陷入了死循环,不知道该怎么解决,爬取的信息也没办法正常保存,求大佬解决,有偿!!!!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



不知道你说的死循环是在哪里,初步判断如果没有保存内容应该是try里有错误导致都走的是
except NoSuchElementException:
                    pass
你可以在pass这里输出信息。另外你需要设置条件跳出while True,否则会一致运行
2020-10-09 10:25
夜莺不会唱歌
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2020-10-7
得分:0 
回复 2楼 fall_bernana
嗯嗯,好嘞,我尝试一下,谢谢您
2020-10-09 16:25



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-503373-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.264655 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved