-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtextMining3.py
113 lines (94 loc) · 3.48 KB
/
textMining3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 12 17:45:27 2018
@author: Utente
"""
def FB_scraper(base_url, path_browser_driver, driver,name):
import time
import pandas as pd
from selenium import webdriver
driver.get(base_url)
name=name
#--------------------------------------------------------------------------------
# INFINITE SCROLLING:
#--------------------------------------------------------------------------------
pause = 5
lastHeight = driver.execute_script("return document.body.scrollHeight")
print (lastHeight)
zeta = 0
#infinite scrolling:
#driver.get_screenshot_as_file("test03_1_"+str(i)+".jpg")
# while True:
# scroll 5 times:
for p in range(0,5):
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(pause)
for i in driver.find_elements_by_class_name("UFIPagerLink"):
try:
i.click()
print(i)
except Exception:
print('yea')
for j in driver.find_elements_by_class_name("see_more_link"):
try:
j.click()
print(j)
except Exception:
print('yea')
newHeight = driver.execute_script("return document.body.scrollHeight")
print(newHeight)
if newHeight == lastHeight:
break
lastHeight = newHeight
zeta +=1
posts = driver.find_elements_by_xpath("//div[@class='_4-u2 _4-u8']")
id_post=[]
times=[]
contenuto=[]
condivisioni=[]
likes=[]
nr_comments=[]
n=0
for p in range(0,len(posts)):
#ora e giorno
try:
data=posts[p].find_element_by_css_selector("abbr._5ptz").get_attribute("title")
times.append(data)
id_post.append(p)
except:
print('recensioni a fianco')
n+=1
#posts text:
try:
contenuti=posts[p].find_element_by_css_selector('div._5pbx.userContent._3576').text
contenuto.append(contenuti)
except:
contenuto.append('none')
#shares nr:
try:
share= posts[p].find_elements_by_css_selector("a._3rwx._42ft")
condivisioni.append(share[0].text)
except IndexError:
condivisioni.append('none')
#likes nr:
try:
like= posts[p].find_elements_by_css_selector("a._3dlf")
likes.append(like[0].text.split('\n')[0])
except IndexError:
likes.append('none')
#comments number:
try:
nrComm= posts[p].find_elements_by_css_selector("a._3hg-._42ft")
nr_comments.append(nrComm[0].text)
except IndexError:
nr_comments.append('none')
#clean for the case of advertise posts:
if len(times)!=len(contenuto) and len(times)!=len(condivisioni):
diff=len(contenuto)-len(times)
contenuto=contenuto[diff:]
condivisioni=condivisioni[diff:]
posts=posts[diff:]
likes=likes[diff:]
nr_comments=nr_comments[diff:]
fb_page=pd.DataFrame({'utente':name,'data':times,'post':contenuto,'id post':id_post,'likes':likes, 'share': condivisioni,'comments number':nr_comments})
return(fb_page)