def execute(username, password): print "launcher start!!!!!!" launcher = Launcher(username, password) print "launcher end!!!!!!!!" driver = launcher.login() es = Es_fb() comment = Comment(username, password) list = comment.get_comment() comment.save('facebook_feedback_comment', 'text', list) friend = Friend(username, password) list = friend.get_friend() friend.save('facebook_feedback_friends', 'text', list) like = Like(username, password) list = like.get_like() like.save('facebook_feedback_like', 'text', list) mention = Mention(username, password) list = mention.get_mention() mention.save('facebook_feedback_at', 'text', list) message = Message(username, password) list = message.get_message() message.save('facebook_feedback_private', 'text', list) online = Online(username, password) share = Share(username, password) list = share.get_share() share.save('facebook_feedback_retweet', 'text', list)
class Online(): def __init__(self, username, password): self.launcher = Launcher(username, password) self.driver = self.launcher.login() def get_online(self): try: name_list = [] status_list = [] for each in self.driver.find_elements_by_xpath( '//li[@class="_42fz"]/a/div/div[3]'): name_list.append(each.text) for each in self.driver.find_elements_by_xpath( '//li[@class="_42fz"]/a/div/div[2]'): try: each.find_element_by_xpath('./div/span') status = 'online' status_list.append(status) except Exception as e: status = 'None' status_list.append(status) z = zip(name_list, status_list) dict = dict({name, status} for name, status in z) print(dict) finally: self.driver.quit() self.launcher.display.popen.kill()
class Userinfo: def __init__(self, username, password): self.launcher = Launcher(username, password) def getUserinfo(self): driver = self.launcher.login() time.sleep(2) driver.find_element_by_xpath('//a[@title="个人主页"]').click() time.sleep(1) driver.find_element_by_xpath('//a[@data-tab-key="about"]').click() time.sleep(1) current_url = driver.current_url pattern0 = re.compile('id=(\d+)') id = re.findall(pattern0, current_url)[0] eachs = [each for each in driver.find_elements_by_xpath('//div[@data-pnref="overview"]//li')] career = eachs[0].text location = eachs[2].text pattern = re.compile(u'(\d+)年') print(eachs[5].text) birth = int(re.findall(pattern,eachs[5].text)[0]) today = int(time.strftime('%Y',time.localtime(int(time.time())))) age = today - birth driver.find_element_by_xpath('//ul[@data-testid="info_section_left_nav"]/li[6]/a').click() time.sleep(1) description = driver.find_element_by_xpath('//div[@id="pagelet_bio"]/div/ul/li').text dict = {'id':id,'career':career,'location':location,'age':age,'description':description} driver.quit() self.launcher.display.popen.kill() return dict
class Like(): def __init__(self, username, password, consumer_key, consumer_secret, access_token, access_secret): self.launcher = Launcher(username, password, consumer_key, consumer_secret, access_token, access_secret) self.driver = self.launcher.login() self.es = Es_twitter() self.api = self.launcher.api() self.driver.get('https://twitter.com/i/notifications') time.sleep(2) self.lis = self.driver.find_elements_by_xpath( '//li[@data-item-type="activity"]') self.list = [] self.update_time = int(time.time()) def get_like(self): try: for li in self.lis: type = li.get_attribute('data-component-context') if type == "favorite_activity": user_name = li.find_element_by_xpath( './div/div/div/div[2]/div[1]/a/strong').text screen_name = li.find_element_by_xpath( './div/div/div/div[2]/div[1]/a').get_attribute( 'href').replace('https:twitter.com/', '') timestamp = li.find_element_by_xpath( './div/div/div/div[2]/div[1]/div[1]/div/span' ).get_attribute('data-time') user_id = li.find_element_by_xpath( './div/div/div/div[2]/div[1]/a').get_attribute( 'data-user-id') root_user_id = li.find_element_by_xpath( './div/div/div/div[2]/div[2]/div/div/div' ).get_attribute('data-user-id') root_content = li.find_element_by_xpath( './div/div/div/div[2]/div[2]/div/div/div/div/div/div[2]' ).text mid = li.get_attribute('data-item-id') photo_url = li.find_element_by_xpath( './div/div/div/div[2]//img').get_attribute('src') item = { 'uid': user_id, 'photo_url': photo_url, 'user_name': screen_name, 'nick_name': user_name, 'timestamp': int(timestamp), 'text': root_content, 'update_time': self.update_time, 'root_text': root_content, 'root_mid': mid } self.list.append(item) finally: self.driver.close() return self.list def save(self, indexName, typeName, list): self.es.executeES(indexName, typeName, list)
class Friend(): def __init__(self, username, password): self.launcher = Launcher(username, password) self.driver,self.display = self.launcher.login() time.sleep(2) # 退出通知弹窗进入页面 try: self.driver.find_element_by_xpath('//div[@class="_n8 _3qx uiLayer _3qw"]').click() except: pass self.driver.find_element_by_xpath('//a[@title="个人主页"]').click() time.sleep(3) # 退出通知弹窗进入页面 try: self.driver.find_element_by_xpath('//div[@class="_n8 _3qx uiLayer _3qw"]').click() except: pass #self.driver.find_element_by_xpath('//ul[@data-referrer="timeline_light_nav_top"]/li[3]/a').click() self.driver.find_element_by_xpath('//ul[@data-referrer="timeline_light_nav_top"]/li[3]/a').click() time.sleep(3) # 退出通知弹窗进入页面 try: self.driver.find_element_by_xpath('//div[@class="_n8 _3qx uiLayer _3qw"]').click() except: pass #加载更多 length=100 for i in range(0,50): js="var q=document.documentElement.scrollTop="+str(length) self.driver.execute_script(js) time.sleep(1) length+=length self.es = Es_fb() self.list = [] self.current_ts = int(time.time()) self.update_time = self.current_ts def get_friend(self): try: for each in self.driver.find_elements_by_xpath('//div[@class="_5h60 _30f"]//ul//li'): try: pic_url = each.find_element_by_xpath('./div/a/img').get_attribute('src') name = each.find_element_by_xpath('./div/div/div[2]/div/div[2]/div/a').text user_id = ''.join(re.findall(re.compile('id=(\d+)'),each.find_element_by_xpath('./div/div/div[2]/div/div[2]/div/a').get_attribute('data-hovercard'))) friends = each.find_element_by_xpath('./div/div/div[2]/div/div[2]/a').text profile_url = each.find_element_by_xpath('./div/div/div[2]/div/div[2]/div/a').get_attribute('href') + '&sk=about' except: pass self.list.append({'uid':user_id, 'photo_url':pic_url, 'nick_name':name, 'friends':friends, 'profile_url':profile_url, 'update_time':self.update_time}) finally: self.driver.quit() self.display.popen.kill() return self.list def save(self, indexName, typeName, list): self.es.executeES(indexName, typeName, list)
class Like(): def __init__(self, username, password): self.launcher = Launcher(username, password) self.driver = self.launcher.login() self.es = Es_twitter() self.api = self.launcher.api() self.driver.get('https://twitter.com/i/notifications') time.sleep(2) self.lis = self.driver.find_elements_by_xpath( '//li[@data-item-type="activity"]') self.list = [] def get_like(self): for li in self.lis: type = li.get_attribute('data-component-context') if type == "favorite_activity": user_name = li.find_element_by_xpath( './div/div/div/div[2]/div[1]/a/strong').text timestamp = li.find_element_by_xpath( './div/div/div/div[2]/div[1]/div[1]/div/span' ).get_attribute('data-time') user_id = li.find_element_by_xpath( './div/div/div/div[2]/div[1]/a').get_attribute( 'data-user-id') root_user_name = li.find_element_by_xpath( './div/div/div/div[2]/div[2]/div/div/div/div/div/div[1]/b' ).text root_user_screen_name = li.find_element_by_xpath( './div/div/div/div[2]/div[2]/div/div/div/div/div/div[1]/span[3]/b' ).text root_user_id = li.find_element_by_xpath( './div/div/div/div[2]/div[2]/div/div/div').get_attribute( 'data-user-id') root_content = li.find_element_by_xpath( './div/div/div/div[2]/div[2]/div/div/div/div/div/div[2]' ).text mid = li.get_attribute('data-item-id') item = { 'nick_name': user_name, 'timestamp': int(timestamp), 'uid': user_id, 'root_user_name': root_user_name, 'root_nick_name': root_user_screen_name, 'root_uid': root_user_id, 'root_text': root_content, 'mid': mid } self.list.append(item) return self.list def save(self, indexName, typeName, list): self.es.executeES(indexName, typeName, list)
class Friend(): def __init__(self): self.fb = Launcher('18538728360', 'zyxing,0513') self.driver = self.fb.login() self.driver.find_element_by_xpath('//a[@title="个人主页"]').click() time.sleep(1) self.driver.find_element_by_xpath( '//ul[@id="u_jsonp_2_8"]/li[3]/a').click() time.sleep(1) self.driver.execute_script(""" (function () { var y = 0; var step = 100; window.scroll(0, 0); function f() { if (y < document.body.scrollHeight) { y += step; window.scroll(0, y); setTimeout(f, 150); } else { window.scroll(0, 0); document.title += "scroll-done"; } } setTimeout(f, 1500); })(); """) time.sleep(3) while True: if "scroll-done" in self.driver.title: break else: time.sleep(3) self.list = [] def get_friend(self): for each in driver.find_elements_by_xpath( '//div[@class="_5h60 _30f"]//ul//li'): try: pic_url = each.find_element_by_xpath( './div/a/img').get_attribute('src') name = each.find_element_by_xpath( './div/div/div[2]/div/div[2]/div/a').text id = ''.join( re.findall( re.compile('id=(\d+)'), each.find_element_by_xpath( './div/div/div[2]/div/div[2]/div/a').get_attribute( 'data-hovercard'))) except Exception as e: pass self.list.append({'pic_url': pic_url, 'name': name, 'id': id}) return self.list
class Userinfo(): def __init__(self, username, password): self.launcher = Launcher(username, password) def getUserinfo(self): driver, display = self.launcher.login() time.sleep(2) driver.find_element_by_xpath( '//a[@data-component-context="home_nav"]').click() time.sleep(2) screen_name_url = driver.find_element_by_xpath( '//div[@class="DashboardProfileCard-content"]//a').get_attribute( 'href') driver.get(screen_name_url) api = self.launcher.api() time.sleep(1) screen_name = driver.find_element_by_xpath( '//div[@class="ProfileHeaderCard"]/h2//b').text uid = api.get_user(screen_name).id try: description = api.get_user(screen_name).description except: description = None try: location = api.get_user(screen_name).location except: location = None today = int(time.strftime('%Y', time.localtime(int(time.time())))) try: birth = driver.find_element_by_xpath( '//span[@class="ProfileHeaderCard-birthdateText u-dir"]/span' ).text except: birth = False pattern = re.compile(u'(\d+)年') pattern1 = re.compile(', (\d+)') if birth: try: birthday = int(re.findall(pattern, birth)[0]) except: birthday = int(re.findall(pattern1, birth)[0]) age = today - birthday else: age = None dict = { 'uid': uid, 'desccription': description, 'location': location, 'age': age } driver.quit() display.popen.kill() return dict
class Share(): def __init__(self, username, password): self.launcher = Launcher(username, password) self.driver = self.launcher.login() self.es = Es_fb() self.list = [] self.share_list = self.launcher.get_share_list() def get_share(self): self.driver.get(self.share_list[0]) for ea in self.driver.find_elements_by_xpath( '//div[@id="repost_view_permalink"]/div/div[1]/div'): for each in ea.find_elements_by_xpath('./div'): author_name = each.find_element_by_xpath( './div/div[2]/div[1]/div[2]/div[1]/div/div/div[2]/div/div/div[2]/h5/span/span/span/a' ).text author_id = re.findall( re.compile('id=(\d+)'), each.find_element_by_xpath( './div/div[2]/div[1]/div[2]/div[1]/div/div/div[2]/div/div/div[2]/h5/span/span/span/a' ).get_attribute('data-hovercard')) pic_url = each.find_element_by_xpath( './div/div[2]/div/div[2]/div/div/a/div/img').get_attribute( 'src') try: content = each.find_element_by_xpath( './div/div[2]/div/div[2]/div[2]//p').text except Exception as e: content = 'None' timestamp = int( each.find_element_by_xpath( './div/div[2]/div/div[2]/div/div/div/div[2]/div/div/div[2]/div/span[3]/span/a/abbr' ).get_attribute('data-utime')) item = { 'nick_name': author_name, 'uid': author_id, 'photo_url': pic_url, 'text': content, 'timestamp': timestamp } self.list.append(item) return self.list def save(self, indexName, typeName, list): self.es.executeES(indexName, typeName, list)
class Message(): def __init__(self,username, password): self.launcher = Launcher(username, password) self.driver = self.launcher.login() self.es = Es_fb() self.list = [] def get_list(self): self.driver.get('https://www.facebook.com/messages/t/') sx_list = [] for each in self.driver.find_elements_by_xpath('//ul[@aria-label="对话列表"]/li'): author_name = each.find_element_by_xpath('./div/a/div[2]/div[1]/span').text pic_url = each.find_element_by_xpath('./div/a/div[1]/div/div/div//img').get_attribute('src') message_url = each.find_element_by_xpath('./div/a').get_attribute('data-href') sx_list.append({'name':author_name,'pic':pic_url,'message_url':message_url}) return sx_list def get_message(self): sx_list = self.get_list() for sx in sx_list: self.driver.get(sx['message_url']) time.sleep(1) for message in self.driver.find_elements_by_xpath('//div[@class="_41ud"]'): try: mes = message.find_element_by_xpath('./div/div/div/span').text except Exception as e: mes = 'None' try: ti = [each for each in self.driver.find_elements_by_xpath('//div[@aria-label="消息"]//time')][-1].text ti = '-'.join([i for i in re.findall(re.compile('(\d+)年(\d+)月(\d+)日'),ti)[0]]) timestamp = int(time.mktime(time.strptime(ti,"%Y-%m-%d"))) except: timestamp = int(time.time()) self.list.append({'nick_name':sx['name'],'text':mes,'timestamp':timestamp}) return self.list def save(self, indexName, typeName, list): self.es.executeES(indexName, typeName, list)
def execute(username, password): launcher = Launcher(username, password) driver = launcher.login() at = At(username, password) list = at.get_mention() at.save('twitter_feedback_at', 'text', list) follower = Follower(username, password) #传uid list = follower.get_follower() follower.save('twitter_feedback_fans', 'text', list) like = Like(username, password) list = like.get_like() like.save('twitter_feedback_like', 'text', list) message = Message(username, password) list = message.get_message() message.save('twitter_feedback_private', 'text', list) share = Share(username, password) list = share.get_share() share.save('twitter_feedback_retweet', 'text', list)
def execute(username, password): launcher = Launcher(username, password, 'N1Z4pYYHqwcy9JI0N8quoxIc1', 'VKzMcdUEq74K7nugSSuZBHMWt8dzQqSLNcmDmpGXGdkH6rt7j2', '943290911039029250-yWtATgV0BLE6E42PknyCH5lQLB7i4lr', 'KqNwtbK79hK95l4X37z9tIswNZSr6HKMSchEsPZ8eMxA9') driver = launcher.login() at = At(username, password, 'N1Z4pYYHqwcy9JI0N8quoxIc1', 'VKzMcdUEq74K7nugSSuZBHMWt8dzQqSLNcmDmpGXGdkH6rt7j2', '943290911039029250-yWtATgV0BLE6E42PknyCH5lQLB7i4lr', 'KqNwtbK79hK95l4X37z9tIswNZSr6HKMSchEsPZ8eMxA9') list = at.get_mention() at.save('twitter_feedback_at','text',list) follower = Follower(username, password, time.time(), 'N1Z4pYYHqwcy9JI0N8quoxIc1', 'VKzMcdUEq74K7nugSSuZBHMWt8dzQqSLNcmDmpGXGdkH6rt7j2', '943290911039029250-yWtATgV0BLE6E42PknyCH5lQLB7i4lr', 'KqNwtbK79hK95l4X37z9tIswNZSr6HKMSchEsPZ8eMxA9') #传uid list = follower.get_follower() follower.save('twitter_feedback_fans','text',list) like = Like(username, password, 'N1Z4pYYHqwcy9JI0N8quoxIc1', 'VKzMcdUEq74K7nugSSuZBHMWt8dzQqSLNcmDmpGXGdkH6rt7j2', '943290911039029250-yWtATgV0BLE6E42PknyCH5lQLB7i4lr', 'KqNwtbK79hK95l4X37z9tIswNZSr6HKMSchEsPZ8eMxA9') list = like.get_like() like.save('twitter_feedback_like','text',list) #message = Message(username, password, 'N1Z4pYYHqwcy9JI0N8quoxIc1', 'VKzMcdUEq74K7nugSSuZBHMWt8dzQqSLNcmDmpGXGdkH6rt7j2', '943290911039029250-yWtATgV0BLE6E42PknyCH5lQLB7i4lr', 'KqNwtbK79hK95l4X37z9tIswNZSr6HKMSchEsPZ8eMxA9') #list = message.get_message() #message.save('twitter_feedback_private','text',list) share = Share(username, password, 'N1Z4pYYHqwcy9JI0N8quoxIc1', 'VKzMcdUEq74K7nugSSuZBHMWt8dzQqSLNcmDmpGXGdkH6rt7j2', '943290911039029250-yWtATgV0BLE6E42PknyCH5lQLB7i4lr', 'KqNwtbK79hK95l4X37z9tIswNZSr6HKMSchEsPZ8eMxA9') list = share.get_share() share.save('twitter_feedback_retweet','text',list)
class Operation(): def __init__(self, username, password): print '11' self.launcher = Launcher(username, password) print '22' self.driver = self.launcher.login() print '33' def publish(self, text): try: # 退出通知弹窗进入页面 time.sleep(1) try: self.driver.find_element_by_xpath( '//div[@class="_n8 _3qx uiLayer _3qw"]').click() except: pass try: self.driver.find_element_by_xpath( '//textarea[@title="分享新鲜事"]').click() self.driver.find_element_by_xpath( '//textarea[@title="分享新鲜事"]').send_keys(text) except: try: self.driver.find_element_by_xpath( '//textarea[@class="_3en1 _480e navigationFocus"]' ).click() self.driver.find_element_by_xpath( '//textarea[@class="_3en1 _480e navigationFocus"]' ).send_keys(text) except: self.driver.find_element_by_xpath( '//div[@class="_1mwp navigationFocus _395 _1mwq _4c_p _5bu_ _34nd _21mu _5yk1"]' ).click() self.driver.find_element_by_xpath( '//div[@class="_1mwp navigationFocus _395 _1mwq _4c_p _5bu_ _34nd _21mu _5yk1"]' ).send_keys(text) try: self.driver.find_element_by_xpath( '//button[@class="_1mf7 _4jy0 _4jy3 _4jy1 _51sy selected _42ft"]' ).click() except: try: self.driver.find_element_by_xpath( '//button[@class="_42ft _4jy0 _ej1 _4jy3 _4jy1 selected _51sy"]' ).click() except: self.driver.find_element_by_xpath( '//button[@data-testid="react-composer-post-button"]' ).click() time.sleep(5) return [True, ''] except Exception as e: return [False, e] finally: self.driver.close() def mention(self, username, text): try: # 退出通知弹窗进入页面 time.sleep(1) try: self.driver.find_element_by_xpath( '//div[@class="_n8 _3qx uiLayer _3qw"]').click() except: pass try: self.driver.find_element_by_xpath( '//textarea[@title="分享新鲜事"]').click() self.driver.find_element_by_xpath( '//textarea[@title="分享新鲜事"]').send_keys(text) except: self.driver.find_element_by_xpath( '//div[@class="_1mwp navigationFocus _395 _1mwq _4c_p _5bu_ _34nd _21mu _5yk1"]' ).click() self.driver.find_element_by_xpath( '//div[@class="_1mwp navigationFocus _395 _1mwq _4c_p _5bu_ _34nd _21mu _5yk1"]' ).send_keys(text) time.sleep(2) try: self.driver.find_element_by_xpath( '//table[@class="uiGrid _51mz _5f0n"]/tbody/tr[3]/td[1]//a/div' ).click() except: self.driver.find_element_by_xpath( '//table[@class="uiGrid _51mz _5f0n"]/tbody/tr[2]/td[2]//a/div' ).click() time.sleep(1) self.driver.find_element_by_xpath( '//input[@aria-label="你和谁一起?"]').send_keys(username) self.driver.find_element_by_xpath( '//input[@aria-label="你和谁一起?"]').send_keys(Keys.ENTER) time.sleep(1) try: self.driver.find_element_by_xpath( '//button[@class="_1mf7 _4jy0 _4jy3 _4jy1 _51sy selected _42ft"]' ).click() except: self.driver.find_element_by_xpath( '//button[@data-testid="react-composer-post-button"]' ).click() time.sleep(5) return [True, ''] except Exception as e: return [False, e] finally: self.driver.close() def follow(self, uid): try: driver = self.launcher.target_page(uid) # 退出通知弹窗进入页面 time.sleep(1) try: self.driver.find_element_by_xpath( '//div[@class="_n8 _3qx uiLayer _3qw"]').click() except: pass try: driver.find_element_by_xpath( '//button[@data-testid="page_profile_follow_button_test_id"]' ).click() except: driver.find_element_by_xpath( '//div[@id="pagelet_timeline_profile_actions"]/div[2]/a[1]' ).click() time.sleep(5) return [True, ''] except Exception as e: return [False, e] finally: driver.close() def not_follow(self, uid): try: driver = self.launcher.target_page(uid) # 退出通知弹窗进入页面 time.sleep(1) try: self.driver.find_element_by_xpath( '//div[@class="_n8 _3qx uiLayer _3qw"]').click() except: pass chain = ActionChains(driver) try: implement = driver.find_element_by_xpath( '//div[@id="pagelet_timeline_profile_actions"]/div[2]/div[1]/div[1]' ) chain.move_to_element(implement).perform() time.sleep(2) implement = driver.find_element_by_xpath( '//div[@id="pagelet_timeline_profile_actions"]/div[2]/div[1]/div[1]' ) chain.move_to_element(implement).perform() time.sleep(2) driver.find_element_by_xpath( '//a[@ajaxify="/ajax/follow/unfollow_profile.php?profile_id=%s&location=1"]' % uid).click() except: try: implement = driver.find_element_by_xpath( '//button[@data-testid="page_profile_follow_button_test_id"]' ) chain.move_to_element(implement).perform() time.sleep(2) implement = driver.find_element_by_xpath( '//button[@data-testid="page_profile_follow_button_test_id"]' ) chain.move_to_element(implement).perform() time.sleep(2) driver.find_element_by_xpath( '//a[@ajaxify="/ajax/follow/unfollow_profile.php?profile_id=%s&location=1"]' % uid).click() except: implement = driver.find_element_by_xpath( '//button[@class="_42ft _4jy0 _3oz- _52-0 _4jy4 _517h _51sy"]' ) chain.move_to_element(implement).perform() time.sleep(2) implement = driver.find_element_by_xpath( '//button[@class="_42ft _4jy0 _3oz- _52-0 _4jy4 _517h _51sy"]' ) chain.move_to_element(implement).perform() time.sleep(2) driver.find_element_by_xpath( '//a[@ajaxify="/ajax/follow/unfollow_profile.php?profile_id=%s&location=1"]' % uid).click() time.sleep(5) return [True, ''] except Exception as e: return [False, e] finally: driver.close() # 私信(未关注) def send_message(self, uid, text): #发送给未关注的用户 try: driver = self.launcher.target_page(uid) message_url = 'https://www.facebook.com/messages/t/' + uid driver.get(message_url) time.sleep(5) # 退出通知弹窗进入页面 time.sleep(1) try: self.driver.find_element_by_xpath( '//div[@class="_n8 _3qx uiLayer _3qw"]').click() except: pass driver.find_element_by_xpath( '//div[@aria-label="输入消息..."]').send_keys(text) driver.find_element_by_xpath( '//div[@aria-label="输入消息..."]').send_keys(Keys.ENTER) time.sleep(5) return [True, ''] except Exception as e: return [False, e] finally: driver.close() # 私信(已关注) # def send_message2(self, uid, text): # #发送给已关注的用户 # try: # driver = self.launcher.target_page(uid) # url = driver.find_element_by_xpath('//a[@class="_51xa _2yfv _3y89"]/a[1]').get_attribute('href') # driver.get('https://www.facebook.com' + url) # time.sleep(4) # driver.find_element_by_xpath('//div[@class="_1mf _1mj"]').click() # driver.find_element_by_xpath('//div[@class="_1mf _1mj"]').send_keys(text) # driver.find_element_by_xpath('//div[@class="_1mf _1mj"]').send_keys(Keys.ENTER) # finally: # driver.close() # 点赞 def like(self, uid, fid): try: post_url = 'https://www.facebook.com/' + uid + '/posts/' + fid video_url = 'https://www.facebook.com/' + uid + '/videos/' + fid self.driver.get(post_url) time.sleep(3) try: # 退出通知弹窗进入页面 time.sleep(1) try: self.driver.find_element_by_xpath( '//div[@class="_n8 _3qx uiLayer _3qw"]').click() except: pass self.driver.find_element_by_xpath( '//div[@aria-label="Facebook 照片剧场模式"]') self.driver.get(video_url) time.sleep(2) # 退出通知弹窗进入页面 time.sleep(1) try: self.driver.find_element_by_xpath( '//div[@class="_n8 _3qx uiLayer _3qw"]').click() except: pass for each in self.driver.find_elements_by_xpath( '//a[@data-testid="fb-ufi-likelink"]'): try: each.click() except: pass except: # 退出通知弹窗进入页面 time.sleep(1) try: self.driver.find_element_by_xpath( '//div[@class="_n8 _3qx uiLayer _3qw"]').click() except: pass for each in self.driver.find_elements_by_xpath( '//a[@data-testid="fb-ufi-likelink"]'): try: each.click() except: pass time.sleep(5) return [True, ''] except Exception as e: return [False, e] finally: self.driver.close() # 评论 def comment(self, uid, fid, text): try: post_url = 'https://www.facebook.com/' + uid + '/posts/' + fid video_url = 'https://www.facebook.com/' + uid + '/videos/' + fid self.driver.get(post_url) time.sleep(3) try: # 退出通知弹窗进入页面 time.sleep(1) try: self.driver.find_element_by_xpath( '//div[@class="_n8 _3qx uiLayer _3qw"]').click() except: pass self.driver.find_element_by_xpath( '//div[@aria-label="Facebook 照片剧场模式"]') self.driver.get(video_url) # 退出通知弹窗进入页面 time.sleep(1) try: self.driver.find_element_by_xpath( '//div[@class="_n8 _3qx uiLayer _3qw"]').click() except: pass time.sleep(3) self.driver.find_element_by_xpath( '//div[@class="UFICommentContainer"]/div/div').click() time.sleep(1) self.driver.find_element_by_xpath( '//div[@class="notranslate _5rpu"]').click() time.sleep(1) self.driver.find_element_by_xpath( '//div[@class="notranslate _5rpu"]').send_keys(text) time.sleep(1) self.driver.find_element_by_xpath( '//div[@class="notranslate _5rpu"]').send_keys(keys.ENTER) except: # 退出通知弹窗进入页面 time.sleep(1) try: self.driver.find_element_by_xpath( '//div[@class="_n8 _3qx uiLayer _3qw"]').click() except: pass time.sleep(3) self.driver.find_element_by_xpath( '//div[@class="UFICommentContainer"]/div/div').click() time.sleep(1) self.driver.find_element_by_xpath( '//div[@class="notranslate _5rpu"]').click() time.sleep(1) self.driver.find_element_by_xpath( '//div[@class="notranslate _5rpu"]').send_keys(text) time.sleep(1) self.driver.find_element_by_xpath( '//div[@class="notranslate _5rpu"]').send_keys(keys.ENTER) time.sleep(5) return [True, ''] except Exception as e: return [False, e] finally: time.sleep(3) self.driver.close() # 分享 def share(self, uid, fid, text): try: print 'uid, fid, text...', uid, fid, text post_url = 'https://www.facebook.com/' + uid + '/posts/' + fid video_url = 'https://www.facebook.com/' + uid + '/videos/' + fid self.driver.get(post_url) time.sleep(3) try: # 退出通知弹窗进入页面 time.sleep(1) try: self.driver.find_element_by_xpath( '//div[@class="_n8 _3qx uiLayer _3qw"]').click() except: pass self.driver.find_element_by_xpath( '//div[@aria-label="Facebook 照片剧场模式"]') self.driver.get(video_url) time.sleep(1) # 退出通知弹窗进入页面 time.sleep(1) try: self.driver.find_element_by_xpath( '//div[@class="_n8 _3qx uiLayer _3qw"]').click() except: pass self.driver.find_element_by_xpath( '//a[@title="发送给好友或发布到你的时间线上。"]').click() self.driver.find_element_by_xpath( '//a[@title="发送给好友或发布到你的时间线上。"]').click() time.sleep(3) self.driver.find_element_by_xpath( '//ul[@class="_54nf"]/li[2]').click() time.sleep(3) try: self.driver.find_element_by_xpath( '//div[@class="notranslate _5rpu"]').click() time.sleep(1) self.driver.find_element_by_xpath( '//div[@class="notranslate _5rpu"]').send_keys(text) time.sleep(1) except: self.driver.find_element_by_xpath( '//div[@class="_1mwp navigationFocus _395 _21mu _5yk1"]/div' ).click() time.sleep(1) self.driver.find_element_by_xpath( '//div[@class="_1mwp navigationFocus _395 _21mu _5yk1"]/div' ).send_keys(text) time.sleep(1) self.driver.find_element_by_xpath( '//button[@data-testid="react_share_dialog_post_button"]' ).click() except: # 退出通知弹窗进入页面 time.sleep(1) try: self.driver.find_element_by_xpath( '//div[@class="_n8 _3qx uiLayer _3qw"]').click() except: pass self.driver.find_element_by_xpath( '//a[@title="发送给好友或发布到你的时间线上。"]').click() self.driver.find_element_by_xpath( '//a[@title="发送给好友或发布到你的时间线上。"]').click() time.sleep(5) self.driver.find_element_by_xpath( '//ul[@class="_54nf"]/li[2]').click() time.sleep(5) try: self.driver.find_element_by_xpath( '//div[@class="notranslate _5rpu"]').click() time.sleep(1) self.driver.find_element_by_xpath( '//div[@class="notranslate _5rpu"]').send_keys(text) time.sleep(1) except: self.driver.find_element_by_xpath( '//div[@class="_1mwp navigationFocus _395 _21mu _5yk1"]/div' ).click() time.sleep(1) self.driver.find_element_by_xpath( '//div[@class="_1mwp navigationFocus _395 _21mu _5yk1"]/div' ).send_keys(text) time.sleep(1) self.driver.find_element_by_xpath( '//button[@data-testid="react_share_dialog_post_button"]' ).click() time.sleep(5) return [True, ''] except Exception as e: return [False, e] finally: self.driver.close() #添加好友 def add_friend(self, uid): try: driver = self.launcher.target_page(uid) driver.find_element_by_xpath( '//button[@class="_42ft _4jy0 FriendRequestAdd addButton _4jy4 _517h _9c6"]' ).click() time.sleep(5) return [True, ''] except Exception as e: return [False, e] finally: driver.close() #确认好友请求 def confirm(self, uid): try: driver = self.launcher.target_page(uid) time.sleep(5) driver.find_element_by_xpath( '//div[@class="incomingButton"]/button').click() time.sleep(1) driver.find_element_by_xpath('//li[@data-label="确认"]/a').click() time.sleep(5) return [True, ''] except Exception as e: return [False, e] finally: driver.close() #删除好友 def delete_friend(self, uid): try: driver = self.launcher.target_page(uid) time.sleep(1) driver.find_element_by_xpath( '//div[@id="pagelet_timeline_profile_actions"]/div/a').click() time.sleep(2) driver.find_element_by_xpath('//li[@data-label="删除好友"]/a').click() time.sleep(5) return [True, ''] except Exception as e: return [False, e] finally: driver.close()
class Comment(): def __init__(self, username, password): self.launcher = Launcher(username, password) self.driver = self.launcher.login() self.es = Es_fb() self.comment_list = self.launcher.get_comment_list() self.list = [] def get_comment(self): for url in self.comment_list: print(url) self.driver.get(url) time.sleep(1) try: root_content = self.driver.find_element_by_xpath( '//div[@role="feed"]/div[1]/div[1]/div[2]/div[1]/div[2]/div[2]' ).text except: root_content = self.driver.find_element_by_xpath( '//div[@role="feed"]/div[1]/div[1]/div[1]/div[1]/div[2]/div[2]' ).text try: root_time = self.driver.find_element_by_xpath( '//abbr[@class="_5ptz"]').get_attribute('data-utime') except: root_time = self.driver.find_element_by_xpath( '//abbr[@class="_5ptz timestamp livetimestamp"]' ).get_attribute('data-utime') for each in self.driver.find_elements_by_xpath( '//div[@aria-label="评论"]'): try: author_name = each.find_element_by_xpath( './div/div/div/div[2]/div/div/div/div/div/span/span[1]/a' ).text except: author_name = each.find_element_by_xpath( './div/div/div/div[2]/div/div/div/span/span[1]/a').text try: author_id = ''.join( re.findall( re.compile('id=(\d+)'), each.find_element_by_xpath( './div/div/div/div[2]/div/div/div/div/div/span/span[1]/a' ).get_attribute('data-hovercard'))) except: author_id = ''.join( re.findall( re.compile('id=(\d+)'), each.find_element_by_xpath( './div/div/div/div[2]/div/div/div/span/span[1]/a' ).get_attribute('data-hovercard'))) pic_url = each.find_element_by_xpath( './div/div/div/div[1]/a/img').get_attribute('src') try: content = each.find_element_by_xpath( './div/div/div/div[2]/div/div/div/div/div/span/span[2]/span/span/span/span' ).text except: content = each.find_element_by_xpath( './div/div/div/div[2]/div/div/div/span/span[2]/span/span/span/span' ).text try: ti = int( each.find_element_by_xpath( './div/div/div/div[2]/div/div/div[2]/span[4]/a/abbr' ).get_attribute('data-utime')) except: ti = int( each.find_element_by_xpath( './div/div/div/div[2]/div/div/div[2]/span[5]/a/abbr' ).get_attribute('data-utime')) self.list.append({ 'nick_name': author_name, 'uid': author_id, 'photo_url': pic_url, 'text': content, 'timestamp': ti }) return self.list def save(self, indexName, typeName, list): self.es.executeES(indexName, typeName, list)
class Like(): def __init__(self, username, password): self.launcher = Launcher(username, password) self.driver = self.launcher.login() self.like_list = self.launcher.get_like_list() self.es = Es_fb() self.list = [] def get_like(self): for url in self.like_list: self.driver.get(url) try: root_name = self.driver.find_element_by_xpath( '//span[@class="fwb"]').text except: root_name = self.driver.find_element_by_xpath( '//span[@class="fwb fcg"]').text try: id = ''.join( re.findall( re.compile('id=(\d+)'), self.driver.find_element_by_xpath( '//span[@class="fwb"]/a').get_attribute( 'data-hovercard'))) except: id = ''.join( re.findall( re.compile('id=(\d+)'), self.driver.find_element_by_xpath( '//span[@class="fwb fcg"]/a').get_attribute( 'data-hovercard'))) try: root_content = self.driver.find_element_by_xpath( '//div[@class="_5pbx userContent _22jv _3576"]/p').text except Exception as e: root_content = 'None' try: timestamp = int( self.driver.find_element_by_xpath( '//abbr[@class="_5ptz"]').get_attribute('data-utime')) except: timestamp = int( self.driver.find_element_by_xpath( '//abbr[@class="_5ptz timestamp livetimestamp"]'). get_attribute('data-utime')) self.driver.get( self.driver.find_element_by_xpath( '//a[@class="_2x4v"]').get_attribute('href')) time.sleep(10) for each in self.driver.find_elements_by_xpath( '//li[@class="_5i_q"]'): author_name = each.find_element_by_xpath( './div/div/div/div[1]/div[2]/div/a').text author_id = ''.join( re.findall( re.compile('id=(\d+)'), each.find_element_by_xpath( './div/div/div/div[1]/div[2]/div/a').get_attribute( 'data-hovercard'))) pic_url = each.find_element_by_xpath( './div/a/div/img').get_attribute('src') try: relationship = each.find_element_by_xpath( './div/div/div/div[2]/div[2]/span/div/a/span[2]/span' ).text except: relationship = "None" item = { 'nick_name': author_name, 'uid': author_id, 'photo_url': pic_url, 'facebook_type': relationship, 'root_name': root_name, 'id': id, 'root_content': root_content, 'timestamp': timestamp } self.list.append(item) return self.list def save(self, indexName, typeName, list): self.es.executeES(indexName, typeName, list)
class Share(): def __init__(self, username, password): self.launcher = Launcher(username, password) self.api = self.launcher.api() self.driver = self.launcher.login() self.driver.get('https://twitter.com/i/notifications') self.es = Es_twitter() self.lis = self.driver.find_elements_by_xpath( '//li[@data-item-type="activity"]') self.list = [] def get_share(self): root_uid = self.api.me().id for li in self.lis: try: type = li.get_attribute('data-component-context') if type == "quote_activity" or type == "retweet_activity": try: mid = li.get_attribute('data-item-id') user_name = li.find_element_by_xpath( './div/div[2]/div[1]/a/span[1]').text #user_screen_name = li.find_element_by_xpath('./div/div[2]/div[1]/a/span[2]').text user_id = li.find_element_by_xpath( './div/div[2]/div[1]/a').get_attribute( 'data-user-id') timestamp = int( li.find_element_by_xpath( './div/div[2]/div[1]/small/a/span'). get_attribute('data-time')) content = li.find_element_by_xpath( './div/div[2]/div[2]/p').text #root_user_name = li.find_element_by_xpath('./div/div[2]/div[3]/div/div/div[1]/div[1]/div[1]/b').text root_user_screen_name = li.find_element_by_xpath( './div/div[2]/div[3]/div/div/div[1]/div[1]/div[1]/span[3]/b' ).text root_content = li.find_element_by_xpath( './div/div[2]/div[3]/div/div[1]/div[1]/div[1]/div[2]' ).text except: mid = li.get_attribute('data-item-id') user_name = li.find_element_by_xpath( './div/div/div/div[2]/div/a').text user_id = li.find_element_by_xpath( './div/div/div/div[2]/div/a').get_attribute( 'data-user-id') timestamp = int( li.find_element_by_xpath( './div/div/div/div[2]/div/div/div/span'). get_attribute('data-time')) content = 'None' root_uid = li.find_element_by_xpath( './div/div/div/div[2]/div[2]/div/div/div' ).get_attribute('data-user-id') root_user_screen_name = li.find_element_by_xpath( './div/div/div/div[2]/div[2]/div/div/div' ).get_attribute('data-screen-name') root_content = li.find_element_by_xpath( './div/div/div/div[2]/div[2]/div/div/div/div/div/div[2]' ).text item = { 'user_name': user_name, #'nick_name':user_screen_name, 'uid': user_id, 'timestamp': timestamp, 'text': content, #'root_user_name':root_user_name, 'root_nick_name': root_user_screen_name, 'root_uid': root_uid, 'root_text': root_content, 'mid': mid } self.list.append(item) except: pass return self.list def save(self, indexName, typeName, list): self.es.executeES(indexName, typeName, list)
class Like(): def __init__(self, username, password): self.launcher = Launcher(username, password) self.driver = self.launcher.login() self.like_list = self.launcher.get_like_list() self.es = Es_fb() self.list = [] self.update_time = int(time.time()) def get_like(self): try: for url in self.like_list: self.driver.get(url) time.sleep(1) # 退出通知弹窗进入页面 try: self.driver.find_element_by_xpath('//div[@class="_n8 _3qx uiLayer _3qw"]').click() except: pass try: text = self.driver.find_element_by_xpath('//div[@class="_5pbx userContent _22jv _3576"]').text except Exception as e: text = 'None' try: try: timestamp = int(self.driver.find_element_by_xpath('//abbr[@class="_5ptz"]').get_attribute('data-utime')) except: timestamp = int(self.driver.find_element_by_xpath('//abbr[@class="_5ptz timestamp livetimestamp"]').get_attribute('data-utime')) except: timestamp = 0 try: mid = ''.join(re.findall(re.compile('/(\d+)'),self.driver.find_element_by_xpath('//a[@class="_5pcq"]').get_attribute('href'))) except: mid = 0 # 进入点赞列表页 self.driver.get(self.driver.find_element_by_xpath('//a[@class="_2x4v"]').get_attribute('href')) time.sleep(5) # 退出通知弹窗进入页面 try: self.driver.find_element_by_xpath('//div[@class="_n8 _3qx uiLayer _3qw"]').click() except: pass for each in self.driver.find_elements_by_xpath('//li[@class="_5i_q"]'): try: author_name = each.find_element_by_xpath('./div/div/div/div[1]/div[2]/div/a').text except: author_name = 'None' try: author_id = ''.join(re.findall(re.compile('id=(\d+)'),each.find_element_by_xpath('./div/div/div/div[1]/div[2]/div/a').get_attribute('data-hovercard'))) except: author_id = 'None' try: pic_url = each.find_element_by_xpath('./div/a/div/img').get_attribute('src') except: pic_url = 'None' item = {'uid':author_id, 'photo_url':pic_url, 'nick_name':author_name, 'timestamp':timestamp, 'text':text, 'update_time':self.update_time, 'root_text':text, 'root_mid':mid} self.list.append(item) finally: self.driver.close() return self.list def save(self, indexName, typeName, list): self.es.executeES(indexName, typeName, list)
class Operation(): def __init__(self, username, password, consumer_key, consumer_secret, access_token, access_secret): self.launcher = Launcher(username, password, consumer_key, consumer_secret, access_token, access_secret) self.api = self.launcher.api() self.list = [] def publish(self, text): try: self.api.update_status(text) time.sleep(2) return [True, ' '] except Exception as e: return [False, e] finally: self.launcher.display.popen.kill() #API.update_with_media(filename[, status][, in_reply_to_status_id][, auto_populate_reply_metadata][, lat][, long][, source][, place_id][, file]) def publish_with_media(self, filename, text): try: self.api.update_with_media(filename, text) time.sleep(2) return [True, ' '] except Exception as e: return [False, e] finally: self.launcher.display.popen.kill() def mention(self, text): #text = '@lvleilei1 test' try: self.api.update_status(text) time.sleep(2) return [True, ' '] except Exception as e: return [False, e] finally: self.launcher.display.popen.kill() def message(self, uid, text): try: print self.api.send_direct_message(uid, text=text) time.sleep(2) return [True, ' '] except Exception as e: return [False, e] finally: self.launcher.display.popen.kill() def follow(self, uid): try: self.api.create_friendship(uid) time.sleep(2) return [True, ' '] except Exception as e: return [False, e] finally: self.launcher.display.popen.kill() def destroy_friendship(self, uid): try: self.api.destroy_friendship(uid) time.sleep(2) return [True, ' '] except Exception as e: return [False, e] finally: self.launcher.display.popen.kill() def do_retweet(self, tid): try: self.api.retweet(tid) time.sleep(2) return [True, ' '] except Exception as e: return [False, e] finally: self.launcher.display.popen.kill() def do_retweet_text(self, uid, tid, text): try: driver = self.launcher.login() screen_name = self.launcher.get_user(uid) post_url = 'https://twitter.com/' + screen_name + '/status/' + tid driver.get(post_url) time.sleep(3) current_url = driver.current_url pattern = re.compile('status/(\d+)') primary_id = ''.join(re.findall(pattern, current_url)).strip() driver.find_element_by_xpath( '//button[@aria-describedby="profile-tweet-action-retweet-count-aria-%s"]' % primary_id).click() time.sleep(3) driver.find_element_by_xpath( '//div[@id="retweet-with-comment"]').click() driver.find_element_by_xpath( '//div[@id="retweet-with-comment"]').send_keys(text) driver.find_element_by_xpath( '//button[@class="EdgeButton EdgeButton--primary retweet-action"]' ).click() time.sleep(2) return [True, ' '] except Exception as e: return [False, e] finally: driver.quit() self.launcher.display.popen.kill() def do_favourite(self, tid): try: self.api.create_favorite(tid) time.sleep(2) return [True, ' '] except Exception as e: return [False, e] finally: self.launcher.display.popen.kill() def do_comment(self, uid, tid, text): try: driver = self.launcher.login() screen_name = self.launcher.get_user(uid) post_url = 'https://twitter.com/' + screen_name + '/status/' + tid driver.get(post_url) time.sleep(1) current_url = driver.current_url pattern = re.compile('status/(\d+)') primary_id = ''.join(re.findall(pattern, current_url)).strip() driver.find_element_by_xpath('//div[@id="tweet-box-reply-to-%s"]' % primary_id).click() driver.find_element_by_xpath('//div[@id="tweet-box-reply-to-%s"]' % primary_id).send_keys(text) time.sleep(1) driver.find_element_by_xpath( '//button[@class="tweet-action EdgeButton EdgeButton--primary js-tweet-btn"]' ).click() time.sleep(2) return [True, ' '] except Exception as e: return [False, e] finally: driver.quit() self.launcher.display.popen.kill()
class Comment(): def __init__(self, username, password): self.launcher = Launcher(username, password) self.driver = self.launcher.login() self.es = Es_fb() self.comment_list = self.launcher.get_comment_list() self.list = [] self.update_time = int(time.time()) def get_comment(self): try: for url in self.comment_list: print(url) self.driver.get(url) time.sleep(1) # 退出通知弹窗进入页面 try: self.driver.find_element_by_xpath( '//div[@class="_n8 _3qx uiLayer _3qw"]').click() except: pass try: try: root_text = self.driver.find_element_by_xpath( '//div[@role="feed"]/div[1]/div[1]/div[2]/div[1]/div[2]/div[2]' ).text except: root_text = self.driver.find_element_by_xpath( '//div[@role="feed"]/div[1]/div[1]/div[1]/div[1]/div[2]/div[2]' ).text except: root_text = 'None' try: try: root_mid = ''.join( re.findall( re.compile('story_fbid=(\d+)'), self.driver.find_element_by_xpath( '//div[@role="feed"]/div[1]/div[1]/div[2]/div[1]/div[2]/div[1]/div/div/div[2]/div/div/div[2]/div/span[3]/span/a' ).get_attribute('href'))) except: root_mid = ''.join( re.findall( re.compile('story_fbid=(\d+)'), self.driver.find_element_by_xpath( '//div[@role="feed"]/div[1]/div[1]/div[1]/div[1]/div[2]/div[1]/div/div/div[2]/div/div/div[2]/div/span[3]/span/a' ).get_attribute('href'))) except: root_mid = 'None' for each in self.driver.find_elements_by_xpath( '//div[@aria-label="评论"]'): try: try: author_name = each.find_element_by_xpath( './div/div/div/div[2]/div/div/div/div/div/span/span[1]/a' ).text except: author_name = each.find_element_by_xpath( './div/div/div/div[2]/div/div/div/span/span[1]/a' ).text except: author_name = 'None' try: try: author_id = ''.join( re.findall( re.compile('id=(\d+)'), each.find_element_by_xpath( './div/div/div/div[2]/div/div/div/div/div/span/span[1]/a' ).get_attribute('data-hovercard'))) except: author_id = ''.join( re.findall( re.compile('id=(\d+)'), each.find_element_by_xpath( './div/div/div/div[2]/div/div/div/span/span[1]/a' ).get_attribute('data-hovercard'))) except: author_id = 'None' try: pic_url = each.find_element_by_xpath( './div/div/div/div[1]/a/img').get_attribute('src') except: pic_url = 'None' try: content = each.find_element_by_xpath( './div/div/div/div[2]/div/div/div/div/div/span/span[2]/span/span/span/span' ).text except: content = each.find_element_by_xpath( './div/div/div/div[2]/div/div/div/span/span[2]/span/span/span/span' ).text try: ti = int( each.find_element_by_xpath( './div/div/div/div[2]/div/div/div[2]/span[4]/a/abbr' ).get_attribute('data-utime')) except: ti = int( each.find_element_by_xpath( './div/div/div/div[2]/div/div/div[2]/span[5]/a/abbr' ).get_attribute('data-utime')) self.list.append({'uid':author_id, 'photo_url':pic_url, 'nick_name':author_name, 'mid':root_mid, 'timestamp':ti, 'text':content,\ 'update_time':self.update_time, 'root_text':root_text, 'root_mid':root_mid}) finally: self.driver.close() return self.list def save(self, indexName, typeName, list): self.es.executeES(indexName, typeName, list)
class Friend(): def __init__(self, username, password): self.launcher = Launcher(username, password) self.driver = self.launcher.login() time.sleep(2) # 退出通知弹窗进入页面 try: self.driver.find_element_by_xpath('//div[@class="_n8 _3qx uiLayer _3qw"]').click() except: pass # # 进入个人主页 # self.driver.find_element_by_xpath('//a[@title="个人主页"]').click() # time.sleep(3) # # 退出通知弹窗进入页面 # try: # self.driver.find_element_by_xpath('//div[@class="_n8 _3qx uiLayer _3qw"]').click() # except: # pass # # 点击好友列表 # self.driver.find_element_by_xpath('//ul[@data-referrer="timeline_light_nav_top"]/li[3]/a').click() # time.sleep(3) # 进入好友请求页面 self.driver.get('https://www.facebook.com/friends/requests') time.sleep(3) # 退出通知弹窗进入页面 try: self.driver.find_element_by_xpath('//div[@class="_n8 _3qx uiLayer _3qw"]').click() except: pass #加载更多 length=100 for i in range(0,20): js="var q=document.documentElement.scrollTop="+str(length) self.driver.execute_script(js) time.sleep(1) length+=400 self.es = Es_fb() self.list = [] self.current_ts = int(time.time()) self.update_time = self.current_ts def get_friend(self): try: # for each in self.driver.find_elements_by_xpath('//div[@class="_5h60 _30f"]//ul//li'): # try: # pic_url = each.find_element_by_xpath('./div/a/img').get_attribute('src') # name = each.find_element_by_xpath('./div/div/div[2]/div/div[2]/div/a').text # user_id = ''.join(re.findall(re.compile('id=(\d+)'),each.find_element_by_xpath('./div/div/div[2]/div/div[2]/div/a').get_attribute('data-hovercard'))) # friends = each.find_element_by_xpath('./div/div/div[2]/div/div[2]/a').text # profile_url = each.find_element_by_xpath('./div/div/div[2]/div/div[2]/div/a').get_attribute('href') + '&sk=about' # except: # pass for each in self.driver.find_elements_by_xpath('//div[@id="globalContainer"]/div/div/div/div/div[3]/div'): try: pic_url = each.find_element_by_xpath('./a/div/img').get_attribute('src') name = each.find_element_by_xpath('./div/div[2]/div[1]/a').text user_id = ''.join(re.findall(re.compile('id=(\d+)'),each.find_element_by_xpath('./div/div[2]/div[1]/a').get_attribute('data-hovercard'))) friends = None profile_url = each.find_element_by_xpath('./div/div[2]/div[1]/a').get_attribute('href') self.list.append({'uid':user_id, 'photo_url':pic_url, 'nick_name':name, 'friends':friends, 'profile_url':profile_url, 'update_time':self.update_time}) except Exception as e: print(e) finally: self.driver.quit() return self.list def save(self, indexName, typeName, list): self.es.executeES(indexName, typeName, list)
from es import Es_fb from launcher import Launcher class Online(): def __init__(self): pass def get_online(self): name_list = [] status_list = [] for each in driver.find_elements_by_xpath( '//li[@class="_42fz"]/a/div/div[3]'): name_list.append(each.text) for each in driver.find_elements_by_xpath( '//li[@class="_42fz"]/a/div/div[2]'): try: each.find_element_by_xpath('./div/span') status = 'online' status_list.append(status) except Exception as e: status = 'None' status_list.append(status) z = zip(name_list, status_list) dict = dict({name, status} for name, status in z) if __name__ == '__main__': fb = Launcher('18538728360', 'zyxing,0513') driver = fb.login() online = Online()
class Operation(): def __init__(self): self.launcher = Launcher('18538728360','zyxing,0513') self.driver = self.launcher.login() def publish(self): self.driver.find_element_by_xpath('//div[@class="_4bl9 _42n-"]').click() time.sleep(2) self.driver.find_element_by_xpath('//div[@class="_1mwp navigationFocus _395 _1mwq _4c_p _5bu_ _34nd _21mu _5yk1"]').send_keys('测试消息3') self.driver.find_element_by_xpath('//button[@class="_1mf7 _4jy0 _4jy3 _4jy1 _51sy selected _42ft"]').click() def mention(self): self.driver.find_element_by_xpath('//div[@class="_4bl9 _42n-"]').click() time.sleep(3) self.driver.find_element_by_xpath('//div[@class="_1mwp navigationFocus _395 _1mwq _4c_p _5bu_ _34nd _21mu _5yk1"]').send_keys('测试消息4') time.sleep(1) self.driver.find_element_by_xpath('//table[@class="uiGrid _51mz _5f0n"]/tbody/tr[2]/td[2]/span/a/div').click() self.driver.find_element_by_xpath('//input[@aria-label="你和谁一起?"]').send_keys('xerxes zhang') self.driver.find_element_by_xpath('//input[@aria-label="你和谁一起?"]').send_keys(Keys.ENTER) time.sleep(1) self.driver.find_element_by_xpath('//button[@class="_1mf7 _4jy0 _4jy3 _4jy1 _51sy selected _42ft"]').click() def follow(self): driver = self.launcher.target_page() driver.find_element_by_xpath('//a[@class="_42ft _4jy0 _4jy4 _517h _51sy"]').click() def not_follow(self): driver = self.launcher.target_page() chain = ActionChains(driver) implement = driver.find_element_by_xpath('//button[@class="_42ft _4jy0 _3oz- _52-0 _4jy4 _517h _51sy"]') chain.move_to_element(implement).perform() driver.find_element_by_xpath('//a[@ajaxify="/ajax/follow/unfollow_profile.php?profile_id=100022568024116&location=1"]').click() # 私信(未关注) def send_message(self): #发送给未关注的用户 driver = self.launcher.target_page() url = driver.find_element_by_xpath('//a[@class="_51xa _2yfv _3y89"]/a[2]').get_attribute('href') driver.get('https://www.facebook.com' + url) driver.find_element_by_xpath('//div[@class="_1mf _1mj"]').send_keys('test') driver.find_element_by_xpath('//div[@class="_1mf _1mj"]').send_keys(Keys.ENTER) # 私信(已关注) def send_message2(self): #发送给已关注的用户 driver = self.launcher.target_page() url = driver.find_element_by_xpath('//a[@class="_51xa _2yfv _3y89"]/a[1]').get_attribute('href') driver.get('https://www.facebook.com' + url) driver.find_element_by_xpath('//div[@class="_1mf _1mj"]').send_keys('test') driver.find_element_by_xpath('//div[@class="_1mf _1mj"]').send_keys(Keys.ENTER) # 测试id #id = 'tl_unit_-8182132709408758851' def like(self, id): driver = self.launcher.target_page() driver.find_element_by_xpath('//div[@id="%s"]/div/div[2]/div[2]/form/div[1]/div[1]/div/div[1]/div/div/div/span[1]/div/a'%id).click() def comment(self, id): driver = self.launcher.target_page() driver.find_element_by_xpath('//div[@id="%s"]/div/div[2]/div[2]/form/div[2]/div/div[2]/div/div[2]/div/div/div/div/div'%id).click() driver.find_element_by_xpath('//div[@id="%s"]/div/div[2]/div[2]/form/div[2]/div/div[2]/div/div[2]/div/div/div/div/div'%id).send_keys('test comment') driver.find_element_by_xpath('//div[@id="%s"]/div/div[2]/div[2]/form/div[2]/div/div[2]/div/div[2]/div/div/div/div/div'%id).send_keys(Keys.ENTER) # 分享 def share(self): driver = self.launcher.target_page() #action_chains = ActionChains(driver) driver.find_element_by_xpath('//div[@id="%s"]/div/div[2]/div[2]/form/div[1]/div[1]/div/div[1]/div/div/div/span[3]//span'%id).click() time.sleep(3) driver.find_element_by_xpath('//div[@id="%s"]/div/div[2]/div[2]/form/div[1]/div[1]/div/div[1]/div/div/div/span[3]/div/span'%id).click() #action_chains.key_down(Keys.CONTROL).click(el) #driver.find_element_by_xpath('//div[@id="%s"]/div/div[2]/div[2]/form/div[1]/div[1]/div/div[1]/div/div/div/span[3]/div/span'%id).click() time.sleep(3) driver.find_element_by_xpath('//ul[@class="_54nf"]/li[2]').click() time.sleep(8) driver.find_element_by_xpath('//div[@class="_1mwp navigationFocus _395 _21mu _5yk1"]').click() driver.find_element_by_xpath('//div[@class="notranslate _5rpu"]/div/div/div').send_keys('test') time.sleep(3) driver.find_element_by_xpath('//button[@data-testid="react_share_dialog_post_button"]').click() # 好友 def friends(self,uid): uid = '100022568024116' self.driver.get('https://www.facebook.com/'+ uid +'/friends') self.driver.execute_script(""" (function () { var y = 0; var step = 100; window.scroll(0, 0); function f() { if (y < document.body.scrollHeight) { y += step; window.scroll(0, y); setTimeout(f, 150); } else { window.scroll(0, 0); document.title += "scroll-done"; } } setTimeout(f, 1500); })(); """) time.sleep(3) while True: if "scroll-done" in driver.title: break else: time.sleep(3) for each in driver.find_elements_by_xpath('//div[@id="contentArea"]/div/div[2]/div/div/div/div[2]/div//ul//li'): try: pic_url = each.find_element_by_xpath('./div/a/img').get_attribute('src') name = each.find_element_by_xpath('./div/div/div[2]/div/div[2]/div/a').text id = ''.join(re.findall(re.compile('id=(\d+)'),each.find_element_by_xpath('./div/div/div[2]/div/div[2]/div/a').get_attribute('data-hovercard'))) except Exception as e: pass
class Mention(): def __init__(self, username, password): self.launcher = Launcher(username, password) self.driver = self.launcher.login() self.mention_list = self.launcher.get_mention_list() self.es = Es_fb() self.list = [] self.update_time = int(time.time()) def get_mention(self): try: for url in self.mention_list: self.driver.get(url) time.sleep(1) # 退出通知弹窗进入页面 try: self.driver.find_element_by_xpath( '//div[@class="_n8 _3qx uiLayer _3qw"]').click() except: pass for each in self.driver.find_elements_by_xpath( '//div[@id="contentArea"]'): try: try: author_name = each.find_element_by_xpath( './div/div/div[3]/div/div/div/div[2]/div[1]/div[2]/div[1]/div/div/div[2]/div/div/div[2]/h5/span/span/span/a' ).text except: author_name = each.find_element_by_xpath( './div/div/div/div/div/div/div[2]/div[1]/div[2]/div[1]/div/div/div[2]/div/div/div[2]/h5/span/span/span/a' ).text except: author_name = 'None' try: try: author_id = ''.join( re.findall( re.compile('id=(\d+)'), each.find_element_by_xpath( './div/div/div[3]/div/div/div/div[2]/div[1]/div[2]/div[1]/div/div/div[2]/div/div/div[2]/h5/span/span/span/a' ).get_attribute('data-hovercard'))) except: author_id = ''.join( re.findall( re.compile('id=(\d+)'), each.find_element_by_xpath( './div/div/div/div/div/div/div[2]/div[1]/div[2]/div[1]/div/div/div[2]/div/div/div[2]/h5/span/span/span/a' ).get_attribute('data-hovercard'))) except: author_id = 'None' try: try: pic_url = each.find_element_by_xpath( './div/div/div[3]/div/div/div/div[2]/div/div[2]/div/div/a/div/img' ).get_attribute('src') except: pic_url = each.find_element_by_xpath( './div/div/div/div/div/div/div[2]/div/div[2]/div/div/a/div/img' ).get_attribute('src') except: pic_url = 'None' try: try: ti = int( each.find_element_by_xpath( './div/div/div[3]/div/div/div/div[2]/div/div[2]/div/div/div/div[2]/div/div/div[2]/div/span[3]/span/span/a/abbr' ).get_attribute('data-utime')) except: ti = int( each.find_element_by_xpath( './div/div/div/div/div/div/div[2]/div/div[2]/div/div/div/div[2]/div/div/div[2]/div/span[3]/span/a/abbr' ).get_attribute('data-utime')) except: ti = 'None' try: content = each.find_element_by_xpath( './div/div/div/div/div/div/div[2]/div/div[2]/div[2]/p' ).text except: content = 'None' try: try: mid = ''.join( re.findall( re.compile('/(\d+)'), each.find_element_by_xpath( './div/div/div[3]/div/div/div/div[2]/div/div[2]/div/div/div/div[2]/div/div/div[2]/div/span[3]/span/span/a' ).get_attribute('href'))) except: mid = ''.join( re.findall( re.compile('/(\d+)'), each.find_element_by_xpath( './div/div/div/div/div/div/div[2]/div/div[2]/div/div/div/div[2]/div/div/div[2]/div/span[3]/span/a' ).get_attribute('href'))) except: mid = 'None' item = { 'uid': author_id, 'photo_url': pic_url, 'nick_name': author_name, 'mid': mid, 'timestamp': ti, 'text': content, 'update_time': self.update_time } self.list.append(item) finally: self.driver.close() return self.list def save(self, indexName, typeName, list): self.es.executeES(indexName, typeName, list)
class Share(): def __init__(self, username, password, consumer_key, consumer_secret, access_token, access_secret): self.launcher = Launcher(username, password, consumer_key, consumer_secret, access_token, access_secret) self.api = self.launcher.api() self.driver, self.display = self.launcher.login() self.driver.get('https://twitter.com/i/notifications') self.es = Es_twitter() self.lis = self.driver.find_elements_by_xpath( '//li[@data-item-type="activity"]') self.list = [] self.update_time = int(time.time()) def get_share(self): try: root_uid = self.api.me().id for li in self.lis: try: type = li.get_attribute('data-component-context') if type == "quote_activity" or type == "retweet_activity": try: mid = li.get_attribute('data-item-id') print "mid", mid user_name = li.find_element_by_xpath( './div/div[2]/div[1]/a/span[1]').text print "user_name", user_name screen_name = li.find_element_by_xpath( './div/div[2]/div[1]/a').get_attribute( 'href').replace('https:twitter.com/', '') print "screen_name", screen_name photo_url = li.find_element_by_xpath( './div/div[2]//img').get_attribute('src') print "photo_url", photo_url user_id = li.find_element_by_xpath( './div/div[2]/div[1]/a').get_attribute( 'data-user-id') print "user_id", user_id timestamp = int( li.find_element_by_xpath( './div/div[2]/div[1]/small/a/span'). get_attribute('data-time')) print "timestamp", timestamp content = li.find_element_by_xpath( './div/div[2]/div[2]/p').text print "content", content root_user_screen_name = li.find_element_by_xpath( './div/div[2]/div[3]/div/div/div[1]/div[1]/div[1]/span[3]/b' ).text print "root_user_screen_name", root_user_screen_name root_content = li.find_element_by_xpath( './div/div[2]/div[3]/div/div[1]/div[1]/div[1]/div[2]' ).text print "root_content", root_content except: mid = li.get_attribute('data-item-id') user_name = li.find_element_by_xpath( './div/div/div/div[2]/div/a').text screen_name = li.find_element_by_xpath( './div/div/div/div[2]/div/a').get_attribute( 'href').replace('https:twitter.com/', '') photo_url = li.find_element_by_xpath( './div/div/div/div[2]//img').get_attribute( 'src') user_id = li.find_element_by_xpath( './div/div/div/div[2]/div/a').get_attribute( 'data-user-id') timestamp = int( li.find_element_by_xpath( './div/div/div/div[2]/div/div/div/span'). get_attribute('data-time')) content = 'None' root_uid = li.find_element_by_xpath( './div/div/div/div[2]/div[2]/div/div/div' ).get_attribute('data-user-id') root_user_screen_name = li.find_element_by_xpath( './div/div/div/div[2]/div[2]/div/div/div' ).get_attribute('data-screen-name') root_content = li.find_element_by_xpath( './div/div/div/div[2]/div[2]/div/div/div/div/div/div[2]' ).text item = { 'uid': user_id, 'photo_url': photo_url, 'user_name': screen_name, 'nick_name': user_name, 'mid': mid, 'timestamp': timestamp, 'text': content, 'update_time': self.update_time, 'root_text': root_content, 'root_mid': mid } self.list.append(item) except: pass finally: self.driver.quit() self.display.popen.kill() return self.list def save(self, indexName, typeName, list): self.es.executeES(indexName, typeName, list)
class Operation(): def __init__(self, username, password): print '11' self.launcher = Launcher(username, password) print '22' self.username = username self.password = password def publish(self, text): driver = self.launcher.login_mobile() time.sleep(20) html = driver.page_source with open("publish1.html", "wb") as f: f.write(html) driver.save_screenshot("publish1.png") driver.find_element_by_xpath('//input[@type="submit"]').click() print "publish111111" driver.find_element_by_xpath( '//textarea[@name="xc_message"]').send_keys(text) print "publish222222" driver.find_element_by_xpath('//input[@name="view_post"]').click() print "publish333333" time.sleep(60) html = driver.page_source with open("publish2.html", "wb") as f: f.write(html) driver.save_screenshot("publish.png") driver.quit() print "publish Success!!!!!!" return [True, ''] def mention(self, username, text): driver = self.launcher.login() #driver,display = self.launcher.login() try: # 退出通知弹窗进入页面 time.sleep(1) try: #driver.find_element_by_xpath('//div[@class="_n8 _3qx uiLayer _3qw"]').click() driver.find_element_by_xpath( '//div[@class="_1k67 _cy7"]').click() print "mention Success11111111111111111111111" except BaseException, e: print "mention Position11111111111111111", e pass try: driver.find_element_by_xpath( '//textarea[@title="分享新鲜事"]').click() driver.find_element_by_xpath( '//textarea[@title="分享新鲜事"]').send_keys(text) except: driver.find_element_by_xpath( '//div[@class="_1mwp navigationFocus _395 _1mwq _4c_p _5bu_ _34nd _21mu _5yk1"]' ).click() driver.find_element_by_xpath( '//div[@class="_1mwp navigationFocus _395 _1mwq _4c_p _5bu_ _34nd _21mu _5yk1"]' ).send_keys(text) time.sleep(2) try: driver.find_element_by_xpath( '//table[@class="uiGrid _51mz"]/tbody/tr[1]/td[1]//a/div' ).click() except: driver.find_element_by_xpath( '//table[@class="uiGrid _51mz _5f0n"]/tbody/tr[2]/td[2]//a/div' ).click() time.sleep(1) driver.find_element_by_xpath( '//input[@aria-label="你和谁一起?"]').send_keys(username) driver.find_element_by_xpath( '//input[@aria-label="你和谁一起?"]').send_keys(Keys.ENTER) time.sleep(1) try: driver.find_element_by_xpath( '//button[@class="_1mf7 _4jy0 _4jy3 _4jy1 _51sy selected _42ft"]' ).click() except: driver.find_element_by_xpath( '//button[@data-testid="react-composer-post-button"]' ).click() time.sleep(5) return [True, ''] except Exception as e: return [False, e]
class Operation(): def __init__(self, username, password): self.launcher = Launcher(username, password) self.driver = self.launcher.login() def publish(self, text): time.sleep(1) try: self.driver.find_element_by_xpath( '//div[@class="_4bl9 _42n-"]').click() except: self.driver.find_element_by_xpath('//div[@class="_4bl9"]').click() time.sleep(2) try: self.driver.find_element_by_xpath( '//textarea[@title="分享新鲜事"]').click() self.driver.find_element_by_xpath( '//textarea[@title="分享新鲜事"]').send_keys(text) except: self.driver.find_element_by_xpath( '//div[@class="_1mwp navigationFocus _395 _1mwq _4c_p _5bu_ _34nd _21mu _5yk1"]' ).click() self.driver.find_element_by_xpath( '//div[@class="_1mwp navigationFocus _395 _1mwq _4c_p _5bu_ _34nd _21mu _5yk1"]' ).send_keys(text) try: self.driver.find_element_by_xpath( '//button[@class="_1mf7 _4jy0 _4jy3 _4jy1 _51sy selected _42ft"]' ).click() except: self.driver.find_element_by_xpath( '//button[@class="_42ft _4jy0 _ej1 _4jy3 _4jy1 selected _51sy"]' ).click() def mention(self, username, text): try: self.driver.find_element_by_xpath( '//div[@class="_4bl9 _42n-"]').click() time.sleep(3) self.driver.find_element_by_xpath( '//div[@class="_1mwp navigationFocus _395 _1mwq _4c_p _5bu_ _34nd _21mu _5yk1"]' ).send_keys(text) time.sleep(1) self.driver.find_element_by_xpath( '//table[@class="uiGrid _51mz _5f0n"]/tbody/tr[2]/td[2]/span/a/div' ).click() self.driver.find_element_by_xpath( '//input[@aria-label="你和谁一起?"]').send_keys(username) self.driver.find_element_by_xpath( '//input[@aria-label="你和谁一起?"]').send_keys(Keys.ENTER) time.sleep(1) self.driver.find_element_by_xpath( '//button[@class="_1mf7 _4jy0 _4jy3 _4jy1 _51sy selected _42ft"]' ).click() except Exception as e: print(e) def follow(self, uid): try: driver = self.launcher.target_page(uid) driver.find_element_by_xpath( '//a[@class="_42ft _4jy0 _4jy4 _517h _51sy"]').click() except Exception as e: print(e) def not_follow(self, uid): try: driver = self.launcher.target_page(uid) chain = ActionChains(driver) implement = driver.find_element_by_xpath( '//button[@class="_42ft _4jy0 _3oz- _52-0 _4jy4 _517h _51sy"]') chain.move_to_element(implement).perform() driver.find_element_by_xpath( '//a[@ajaxify="/ajax/follow/unfollow_profile.php?profile_id=100022934584514&location=1"]' ).click() except Exception as e: print(e) # 私信(未关注) def send_message(self, uid, text): #发送给未关注的用户 try: driver = self.launcher.target_page(uid) message_url = 'https://www.facebook.com/messages/t/' + uid driver.get(message_url) time.sleep(5) driver.find_element_by_xpath( '//div[@aria-label="输入消息..."]').send_keys(text) driver.find_element_by_xpath( '//div[@aria-label="输入消息..."]').send_keys(Keys.ENTER) except Exception as e: print(e) # 私信(已关注) def send_message2(self, uid, text): #发送给已关注的用户 try: driver = self.launcher.target_page(uid) url = driver.find_element_by_xpath( '//a[@class="_51xa _2yfv _3y89"]/a[1]').get_attribute('href') driver.get('https://www.facebook.com' + url) time.sleep(4) driver.find_element_by_xpath('//div[@class="_1mf _1mj"]').click() driver.find_element_by_xpath( '//div[@class="_1mf _1mj"]').send_keys(text) driver.find_element_by_xpath( '//div[@class="_1mf _1mj"]').send_keys(Keys.ENTER) except Exception as e: print(e) ######### # 点赞 def like(self, uid, fid): post_url = 'https://www.facebook.com/' + uid + '/posts/' + fid video_url = 'https://www.facebook.com/' + uid + '/videos/' + fid self.driver.get(post_url) time.sleep(5) try: self.driver.find_element_by_xpath( '//div[@aria-label="Facebook 照片剧场模式"]') self.driver.get(video_url) time.sleep(2) for each in self.driver.find_elements_by_xpath( '//a[@data-testid="fb-ufi-likelink"]'): try: each.click() except: pass except: for each in self.driver.find_elements_by_xpath( '//a[@data-testid="fb-ufi-likelink"]'): try: each.click() except: pass # 评论 def comment(self, uid, fid, text): post_url = 'https://www.facebook.com/' + uid + '/posts/' + fid video_url = 'https://www.facebook.com/' + uid + '/videos/' + fid self.driver.get(post_url) time.sleep(3) try: self.driver.find_element_by_xpath( '//div[@aria-label="Facebook 照片剧场模式"]') self.driver.get(video_url) time.sleep(3) self.driver.find_element_by_xpath( '//div[@class="UFICommentContainer"]/div/div').click() time.sleep(1) self.driver.find_element_by_xpath( '//div[@class="UFICommentContainer"]/div/div').send_keys(text) self.driver.find_element_by_xpath( '//div[@class="UFICommentContainer"]/div/div').send_keys( Keys.ENTER) except: self.driver.find_element_by_xpath( '//div[@class="UFICommentContainer"]/div/div').click() time.sleep(1) self.driver.find_element_by_xpath( '//div[@class="UFICommentContainer"]/div/div').send_keys(text) self.driver.find_element_by_xpath( '//div[@class="UFICommentContainer"]/div/div').send_keys( Keys.ENTER) # 分享 def share(self, uid, fid, text): print 'uid, fid, text...', uid, fid, text post_url = 'https://www.facebook.com/' + uid + '/posts/' + fid video_url = 'https://www.facebook.com/' + uid + '/videos/' + fid self.driver.get(post_url) time.sleep(3) try: self.driver.find_element_by_xpath( '//div[@aria-label="Facebook 照片剧场模式"]') self.driver.get(video_url) time.sleep(1) self.driver.find_element_by_xpath( '//a[@title="发送给好友或发布到你的时间线上。"]').click() self.driver.find_element_by_xpath( '//a[@title="发送给好友或发布到你的时间线上。"]').click() time.sleep(3) self.driver.find_element_by_xpath( '//ul[@class="_54nf"]/li[2]').click() time.sleep(3) try: self.driver.find_element_by_xpath( '//div[@class="_1mwp navigationFocus _395 _21mu _5yk1"]/div' ).click() time.sleep(1) self.driver.find_element_by_xpath( '//div[@class="_1mwp navigationFocus _395 _21mu _5yk1"]/div' ).send_keys(text) time.sleep(1) except: self.driver.find_element_by_xpath( '//div[@class="notranslate _5rpu"]').click() time.sleep(1) self.driver.find_element_by_xpath( '//div[@class="notranslate _5rpu"]').send_keys(text) time.sleep(1) self.driver.find_element_by_xpath( '//button[@data-testid="react_share_dialog_post_button"]' ).click() except: self.driver.find_element_by_xpath( '//a[@title="发送给好友或发布到你的时间线上。"]').click() self.driver.find_element_by_xpath( '//a[@title="发送给好友或发布到你的时间线上。"]').click() time.sleep(5) self.driver.find_element_by_xpath( '//ul[@class="_54nf"]/li[2]').click() time.sleep(5) try: self.driver.find_element_by_xpath( '//div[@class="_1mwp navigationFocus _395 _21mu _5yk1"]/div' ).click() time.sleep(1) self.driver.find_element_by_xpath( '//div[@class="_1mwp navigationFocus _395 _21mu _5yk1"]/div' ).send_keys(text) time.sleep(1) except: self.driver.find_element_by_xpath( '//div[@class="notranslate _5rpu"]').click() time.sleep(1) self.driver.find_element_by_xpath( '//div[@class="notranslate _5rpu"]').send_keys(text) time.sleep(1) self.driver.find_element_by_xpath( '//button[@data-testid="react_share_dialog_post_button"]' ).click() #添加好友 def add_friend(self, uid): try: driver = self.launcher.target_page(uid) driver.find_element_by_xpath( '//button[@class="_42ft _4jy0 FriendRequestAdd addButton _4jy4 _517h _9c6"]' ).click() except Exception as e: print(e) #确认好友请求 def confirm(self, uid): try: driver = self.launcher.target_page(uid) time.sleep(5) driver.find_element_by_xpath( '//div[@class="incomingButton"]/button').click() time.sleep(1) driver.find_element_by_xpath('//li[@data-label="确认"]/a').click() except Exception, e: print e
class Message(): def __init__(self,username, password): self.launcher = Launcher(username, password) self.driver = self.launcher.login() self.es = Es_fb() self.list = [] self.update_time = int(time.time()) def get_list(self): self.driver.get('https://www.facebook.com/messages/t/') # 退出通知弹窗进入页面 try: self.driver.find_element_by_xpath('//div[@class="_n8 _3qx uiLayer _3qw"]').click() except: pass sx_list = [] for each in self.driver.find_elements_by_xpath('//ul[@aria-label="对话列表"]/li'): try: author_name = each.find_element_by_xpath('./div/a/div[2]/div[1]/span').text except: author_name = 'None' try: author_id = ''.join(re.findall(re.compile('row_header_id_user:(\d+)'),each.find_element_by_xpath('./div').get_attribute('id'))) except: author_id = 'None' try: pic_url = each.find_element_by_xpath('./div/a/div[1]/div/div/div//img').get_attribute('src') except: pic_url = 'None' try: message_url = each.find_element_by_xpath('./div/a').get_attribute('data-href') except: message_url = False if message_url: sx_list.append({'name':author_name, 'pic':pic_url, 'message_url':message_url, 'author_id':author_id}) return sx_list def get_message(self): try: sx_list = self.get_list() for sx in sx_list: self.driver.get(sx['message_url']) time.sleep(1) # 退出通知弹窗进入页面 try: self.driver.find_element_by_xpath('//div[@class="_n8 _3qx uiLayer _3qw"]').click() except: pass for message in self.driver.find_elements_by_xpath('//div[@class="_41ud"]'): try: ymd = '-'.join([t for t in re.findall(re.compile('(\d+)年(\d+)月(\d+)日'),message.find_element_by_xpath('./div/div').get_attribute('data-tooltip-content'))[0]]) hm = ':'.join([q for q in re.findall(re.compile('(\d+):(\d+)'),message.find_element_by_xpath('./div/div').get_attribute('data-tooltip-content'))[0]]) messagetime = ymd + ' ' + hm + ':00' messageTime = int(time.mktime(time.strptime(messagetime,'%Y-%m-%d %H:%M:%S'))) except: messageTime = 0 try: messageId = re.findall(re.compile('"fbid:(\d+)"'),message.find_element_by_xpath('./div/div').get_attribute('participants'))[-1] if messageId == sx['author_id']: private_type = 'receive' text = message.text root_text = 'None' else: private_type = 'make' text = 'None' root_text = message.text except: private_type = 'unknown' text = 'None' root_text = 'None' self.list.append({'uid':sx['author_id'], 'photo_url':sx['pic'], 'nick_name':sx['name'], 'timestamp':messageTime, 'update_time':self.update_time, 'text':text, 'root_text':root_text, 'private_type':private_type}) finally: self.driver.close() return self.list def save(self, indexName, typeName, list): self.es.executeES(indexName, typeName, list)
class Friend(): def __init__(self, username, password): self.launcher = Launcher(username, password) self.driver = self.launcher.login() time.sleep(2) self.driver.find_element_by_xpath('//a[@title="个人主页"]').click() time.sleep(3) self.driver.find_element_by_xpath( '//ul[@data-referrer="timeline_light_nav_top"]/li[3]/a').click() time.sleep(1) self.driver.execute_script(""" (function () { var y = 0; var step = 100; window.scroll(0, 0); function f() { if (y < document.body.scrollHeight) { y += step; window.scroll(0, y); setTimeout(f, 150); } else { window.scroll(0, 0); document.title += "scroll-done"; } } setTimeout(f, 1500); })(); """) time.sleep(3) while True: if "scroll-done" in self.driver.title: break else: time.sleep(3) self.data_gt = self.driver.find_element_by_xpath( '//div[@id="contentArea"]/div[1]').get_attribute('data-gt') self.root_uid = json.loads(self.data_gt)['profile_owner'] self.es = Es_fb() self.list = [] self.current_ts = int(time.time()) self.update_time = self.current_ts def get_friend(self): for each in self.driver.find_elements_by_xpath( '//div[@class="_5h60 _30f"]//ul//li'): try: pic_url = each.find_element_by_xpath( './div/a/img').get_attribute('src') name = each.find_element_by_xpath( './div/div/div[2]/div/div[2]/div/a').text user_id = ''.join( re.findall( re.compile('id=(\d+)'), each.find_element_by_xpath( './div/div/div[2]/div/div[2]/div/a').get_attribute( 'data-hovercard'))) update_time = self.update_time except Exception as e: pass self.list.append({ 'root_uid': self.root_uid, 'photo_url': pic_url, 'nick_name': name, 'uid': user_id, 'update_time': update_time }) return self.list def save(self, indexName, typeName, list): self.es.executeES(indexName, typeName, list)
class Mention(): def __init__(self, username, password): self.launcher = Launcher(username, password) self.driver = self.launcher.login() self.mention_list = self.launcher.get_mention_list() self.es = Es_fb() self.list = [] def get_mention(self): for url in self.mention_list: print(url) self.driver.get(url) for each in self.driver.find_elements_by_xpath( '//div[@id="contentArea"]'): try: author_name = each.find_element_by_xpath( './div/div/div[3]/div/div/div/div[2]/div[1]/div[2]/div[1]/div/div/div[2]/div/div/div[2]/h5/span/span/span/a' ).text except: author_name = each.find_element_by_xpath( './div/div/div/div/div/div/div[2]/div[1]/div[2]/div[1]/div/div/div[2]/div/div/div[2]/h5/span/span/span/a' ).text try: author_id = ''.join( re.findall( re.compile('id=(\d+)'), each.find_element_by_xpath( './div/div/div[3]/div/div/div/div[2]/div[1]/div[2]/div[1]/div/div/div[2]/div/div/div[2]/h5/span/span/span/a' ).get_attribute('data-hovercard'))) except: author_id = ''.join( re.findall( re.compile('id=(\d+)'), each.find_element_by_xpath( './div/div/div/div/div/div/div[2]/div[1]/div[2]/div[1]/div/div/div[2]/div/div/div[2]/h5/span/span/span/a' ).get_attribute('data-hovercard'))) try: pic_url = each.find_element_by_xpath( './div/div/div[3]/div/div/div/div[2]/div/div[2]/div/div/a/div/img' ).get_attribute('src') except: pic_url = each.find_element_by_xpath( './div/div/div/div/div/div/div[2]/div/div[2]/div/div/a/div/img' ).get_attribute('src') try: ti = int( each.find_element_by_xpath( './div/div/div[3]/div/div/div/div[2]/div/div[2]/div/div/div/div[2]/div/div/div[2]/div/span[3]/span/span/a/abbr' ).get_attribute('data-utime')) except: ti = int( each.find_element_by_xpath( './div/div/div/div/div/div/div[2]/div/div[2]/div/div/div/div[2]/div/div/div[2]/div/span[3]/span/a/abbr' ).get_attribute('data-utime')) try: content = each.find_element_by_xpath( './div/div/div/div/div/div/div[2]/div/div[2]/div[2]/p' ).text except Exception as e: content = 'None' item = { 'nick_name': author_name, 'uid': author_id, 'photo_url': pic_url, 'timestamp': ti, 'text': content } self.list.append(item) return self.list def save(self, indexName, typeName, list): self.es.executeES(indexName, typeName, list)