@@ -48,7 +48,7 @@ def login(self):
48
48
self .driver .get (self .start_url )
49
49
50
50
WebDriverWait (self .driver , 6 ).until (
51
- EC .presence_of_element_located ((By .CSS_SELECTOR , 'div[id="MComposer "]' )))
51
+ EC .presence_of_element_located ((By .CSS_SELECTOR , 'input[data-testid="search_input "]' )))
52
52
logger .info ("login success by cookies! account={}" .format (self .account ))
53
53
return True , 0
54
54
except Exception as e :
@@ -111,6 +111,7 @@ def add_friends(self, search_keys, limit=2):
111
111
try :
112
112
limit = 1 if limit <= 0 else limit
113
113
logger .info ('增加好友: friends={}, limit={}' .format (search_keys , limit ))
114
+ result = {item : 0 for item in search_keys }
114
115
for friend in search_keys :
115
116
page_url = "https://www.facebook.com/search/people/?q={}&epa=SERP_TAB" .format (friend )
116
117
self .driver .get (page_url )
@@ -120,7 +121,7 @@ def add_friends(self, search_keys, limit=2):
120
121
EC .presence_of_element_located ((By .CSS_SELECTOR , "div[class='FriendButton'" )))
121
122
122
123
# 找到新的好友列表
123
- self .browse_home ( self . driver , browse_times = 5 , distance = 0 , interval = 5 , back_top = True )
124
+ self .browse_page ( browse_times = 5 , distance = 0 , interval = 5 , back_top = True )
124
125
new_friends = self .driver .find_elements_by_css_selector ("button[aria-label='Add Friend'" )
125
126
if not new_friends :
126
127
logger .warning ('增加好友: can not find any friend. friend keyword={}' .format (friend ))
@@ -141,7 +142,8 @@ def add_friends(self, search_keys, limit=2):
141
142
finally :
142
143
self .click (idx , self .driver )
143
144
time .sleep (3 )
144
- logger .info ("增加好友: add friend succeed." )
145
+ result [friend ] += 1
146
+ logger .info ("增加好友: 增加结果为-->{}" .format (result ))
145
147
self .driver .get (self .start_url )
146
148
time .sleep (5 )
147
149
return True , 0
@@ -234,7 +236,7 @@ def post_status(self, contents):
234
236
"""
235
237
236
238
try :
237
- logger .info ("发送状态功能: sentence ={}," .format (contents ,))
239
+ logger .info ("发送状态功能: contents ={}," .format (contents ,))
238
240
message_url = "https://www.facebook.com/home.php?sk=h_chr&ref=bookmarks"
239
241
self .driver .get (message_url )
240
242
@@ -261,7 +263,8 @@ def post_status(self, contents):
261
263
try :
262
264
if "What's on your mind" in row .text :
263
265
try :
264
- self .send_keys (row ,contents )
266
+ row .send_keys (contents )
267
+ # self.send_keys(row,contents)
265
268
except Exception as e :
266
269
continue
267
270
else :
@@ -325,6 +328,43 @@ def browse_user_center(self, limit=3):
325
328
logger .exception ("user_home browsing failed error ={}" .format (e ))
326
329
return self .fb_exp .auto_process (3 )
327
330
331
+ def browse_page (self , browse_times = 0 , distance = 0 , interval = 0 , back_top = True ):
332
+ """
333
+ 浏览页面
334
+ :param driver: 浏览器驱动
335
+ :param browse_times: 浏览次数
336
+ :param distance: 每次间隔距离,默认为零,代表使用随机距离
337
+ :param interval: 间隔时间, 单位秒, 默认为零,代表使用随机停顿时间
338
+ :param back_top: 是否回到顶点
339
+ :return:
340
+ """
341
+ # 浏览页面js
342
+ try :
343
+ logger .info ('browse_page start.' )
344
+ y_dis = 0
345
+ if browse_times <= 0 :
346
+ browse_times = random .randint (3 , 15 )
347
+
348
+ for i in range (browse_times ):
349
+ if interval <= 0 :
350
+ time .sleep (random .randint (1 , 10 ))
351
+ else :
352
+ time .sleep (interval )
353
+
354
+ if distance > 0 :
355
+ y_dis += distance
356
+ else :
357
+ y_dis += random .randint (20 , 200 )
358
+
359
+ self .driver .execute_script ("window.scrollTo(0,{})" .format (y_dis ))
360
+
361
+ if back_top :
362
+ self .driver .execute_script ("window.scrollTo(0,0)" )
363
+ return True
364
+ except Exception as e :
365
+ logger .exception ('browse_page exception. e={}' .format (e ))
366
+ return self .fb_exp .auto_process (3 )
367
+
328
368
329
369
if __name__ == '__main__' :
330
370
filename = '../../resource/facebook_account.txt'
@@ -350,14 +390,28 @@ def browse_user_center(self, limit=3):
350
390
fma .browse_user_center (3 )
351
391
time .sleep (100 )
352
392
353
- # if __name__ == '__main__':
354
- # user_account = str(17610069110)
355
- # user_password = str("sanmang111..fb").strip()
356
- #
357
- # # 登陆
358
- # fma = FacebookPCActions(account_info={"account": user_account, "password": user_password}, finger_print={"user_agent": ""}, headless=False)
359
- # if not fma.start_chrome():
360
- # print("start chrome failed")
361
- # fma.set_exception_processor(
362
- # FacebookExceptionProcessor(fma.driver, env="pc", account=fma.account, gender=fma.gender))
363
- # res, status = fma.login()
393
+
394
+ if __name__ == '__main__' :
395
+ user_account = str (17610069110 )
396
+ user_password = str ("sanmang111..fb" ).strip ()
397
+
398
+ # 初始化
399
+ cookies = [{'domain' : '.facebook.com' , 'httpOnly' : False , 'name' : 'presence' , 'path' : '/' , 'secure' : True , 'value' : 'EDvF3EtimeF1556521496EuserFA21B35381807782A2EstateFDt3F_5b_5dG556521496536CEchFDp_5f1B35381807782F2CC' }, {'domain' : '.facebook.com' , 'expiry' : 1564297494.289986 , 'httpOnly' : True , 'name' : 'fr' , 'path' : '/' , 'secure' : True , 'value' : '14Cgytn5XS9ADvrMS.AWWAmG4Z8RUATBEoDXsVDoRqB1g.BcxqHO.Is.FzG.0.0.BcxqIW.AWVueXRy' }, {'domain' : '.facebook.com' , 'expiry' : 1564297485.612012 , 'httpOnly' : True , 'name' : 'xs' , 'path' : '/' , 'secure' : True , 'value' : '31%3AzbNHAuA7vvOB-Q%3A2%3A1556521482%3A-1%3A-1' }, {'domain' : '.facebook.com' , 'expiry' : 1557126281 , 'httpOnly' : False , 'name' : 'dpr' , 'path' : '/' , 'secure' : True , 'value' : '2' }, {'domain' : '.facebook.com' , 'expiry' : 1619593469.742837 , 'httpOnly' : True , 'name' : 'datr' , 'path' : '/' , 'secure' : True , 'value' : 'zqHGXKNTrxN8ELHDhr2hXAEo' }, {'domain' : '.facebook.com' , 'expiry' : 1564297485.611967 , 'httpOnly' : False , 'name' : 'c_user' , 'path' : '/' , 'secure' : True , 'value' : '100035381807782' }, {'domain' : '.facebook.com' , 'expiry' : 1556611487.566037 , 'httpOnly' : True , 'name' : 'spin' , 'path' : '/' , 'secure' : True , 'value' : 'r.1000651590_b.trunk_t.1556521485_s.1_v.2_' }, {'domain' : '.facebook.com' , 'expiry' : 1557126293 , 'httpOnly' : False , 'name' : 'wd' , 'path' : '/' , 'secure' : True , 'value' : '1200x754' }, {'domain' : '.facebook.com' , 'expiry' : 1619593485.611922 , 'httpOnly' : True , 'name' : 'sb' , 'path' : '/' , 'secure' : True , 'value' : 'zqHGXDSVQ0-0sXJTMh0cHnju' }]
400
+
401
+ fma = FacebookPCActions (account_info = {"account" : user_account , "password" : user_password , "cookies" : cookies }, finger_print = {"user_agent" : "" }, headless = False )
402
+ if not fma .start_chrome ():
403
+ print ("start chrome failed" )
404
+ fma .set_exception_processor (
405
+ FacebookExceptionProcessor (fma .driver , env = "pc" , account = fma .account , gender = fma .gender ))
406
+ # 登陆
407
+ res , status = fma .login ()
408
+
409
+ # 浏览页面
410
+ #fma.browse_home()
411
+ # 增加好友
412
+ #fma.add_friends(["pig","dog"], 2)
413
+ # 发送状态
414
+ #fma.post_status("wo shi yi zhi xiaoxiaoniao 鸟")
415
+ #
416
+
417
+
0 commit comments