2
2
from selenium import webdriver
3
3
from selenium .common .exceptions import NoSuchElementException
4
4
from selenium .webdriver .common .keys import Keys
5
+ from selenium .webdriver .common .desired_capabilities import DesiredCapabilities
5
6
import time
6
7
import os
7
8
import json
@@ -46,7 +47,7 @@ class Post():
46
47
47
48
def __init__ (self ):
48
49
self .posterName = ""
49
- self .description = ""
50
+ self .text = ""
50
51
self .numLikes = 0
51
52
self .time = ""
52
53
self .privacy = ""
@@ -77,7 +78,7 @@ def to_json(self):
77
78
78
79
def __str__ (self ):
79
80
s = "\n Post by " + self .posterName + ": "
80
- s += self .description + "\n "
81
+ s += self .text + "\n "
81
82
s += "Likes: " + str (self .numLikes ) + " - "
82
83
s += "Comments: " + str (self .numComents ) + " - "
83
84
s += self .time + " "
@@ -89,6 +90,13 @@ def __repr__(self):
89
90
return self .__str__ ()
90
91
91
92
93
+ dcap = dict (DesiredCapabilities .PHANTOMJS )
94
+ dcap ["phantomjs.page.settings.userAgent" ] = (
95
+ "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/53 "
96
+ "(KHTML, like Gecko) Chrome/15.0.87"
97
+ )
98
+
99
+
92
100
class FacebookBot (webdriver .PhantomJS ):
93
101
"""Main class for browsing facebook"""
94
102
@@ -104,7 +112,7 @@ def __init__(self):
104
112
path = pathToPhantomJs
105
113
webdriver.PhantomJS.__init__(self, path, service_args=service_args)
106
114
"""
107
- webdriver .PhantomJS .__init__ (self )
115
+ webdriver .PhantomJS .__init__ (self , desired_capabilities = dcap )
108
116
109
117
def get (self , url ):
110
118
"""The make the driver go to the url but reformat the url if is for facebook page"""
@@ -193,13 +201,15 @@ def newMessageToFriend(
193
201
send .send_keys (Keys .ENTER )
194
202
return True
195
203
196
- def getPostInGroup (self , url , deep = 2 ):
197
- """Get a list of posts (list:Post) in group url(str) iterating deep(int) times in the group"""
204
+ def getPostInGroup (self , url , deep = 2 , moreText = "Ver más publicaciones" ):
205
+ """Get a list of posts (list:Post) in group url(str) iterating deep(int) times in the group
206
+ pass moreText depending of your language, i couldn't find a elegant solution for this"""
198
207
199
208
self .get (url )
200
209
ids = [4 , 5 , 6 , 7 , 9 ]
201
210
posts = []
202
211
for n in range (deep ):
212
+ #print("Searching, deep ",n)
203
213
for i in ids :
204
214
# print(i)
205
215
post = Post ()
@@ -212,7 +222,7 @@ def getPostInGroup(self, url, deep=2):
212
222
post .numLikes = int (a [3 ].text .split (" " )[0 ])
213
223
except ValueError :
214
224
post .numLikes = 0
215
- # post.description = p.find_element_by_tag_name("p").text
225
+ post .text = p .find_element_by_tag_name ("p" ).text
216
226
post .time = p .find_element_by_tag_name ("abbr" ).text
217
227
# p.text.split("· ")[1].split("\n")[0]
218
228
post .privacy = self .title
@@ -227,18 +237,18 @@ def getPostInGroup(self, url, deep=2):
227
237
# post.linkToShare = a[5].get_attribute('href')
228
238
post .linkToLikers = a [1 ].get_attribute ('href' )
229
239
post .linkToMore = a [6 ].get_attribute ('href' )
230
- posts .append (post )
240
+ if post not in posts :
241
+ posts .append (post )
231
242
except Exception :
232
243
continue
233
244
try :
234
- more = self .find_element_by_class_name ( "dm" ). find_elements_by_tag_name ( "a" )[
235
- 0 ] .get_attribute ('href' )
245
+ more = self .find_element_by_partial_link_text (
246
+ moreText ) .get_attribute ('href' )
236
247
self .get (more )
237
- except Exception :
238
- pass
239
- #print("can't get more :(")
240
- # return posts, self.getScrenshotName("PostsIn" + self.title,
241
- # screenshot, screenshotPath)
248
+ # self.find_element_by_partial_link_text(moreText)
249
+ except Exception as e :
250
+ print (e )
251
+ print (" Can't get more posts" )
242
252
return posts
243
253
244
254
def postInGroup (self , groupURL , text ):
0 commit comments