-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfb_data.py
121 lines (71 loc) · 2.88 KB
/
fb_data.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
114
115
116
117
118
119
120
121
import json
import csv
import pandas as pd
import facebook
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
import re
from tabulate import tabulate
from time import sleep
def main():
token = ""#your_token
node = "" #group_id
graph = facebook.GraphAPI(token)
fields = ['videos{comments}']
videos = graph.get_object(node,fields=fields)
comment_data = videos['videos']['data']
# print(json.dumps(videos['videos']['data'],indent=4))
#code for structured csv
email = ""#facebook_email_id
passwd = ""#facebook password
d = {}
url = f"https://mobile.facebook.com/groups/{group_id}?refid=27"
option = webdriver.ChromeOptions()
option.add_argument("-incognito")
option.add_argument('--headless')
driver = webdriver.Chrome(executable_path='chromedriver',chrome_options=option)
driver.implicitly_wait(30)
driver.get(url)
username = driver.find_element_by_id('m_login_email')
username.send_keys(email)
password =driver.find_element_by_id('m_login_password')
password.send_keys(passwd)
login = driver.find_element_by_name('login')
login.click()
driver.implicitly_wait(20)
def get_name(comment_id):
if(comment_id in d):
return d[comment_id]
else:
url_2 = f"https://mobile.facebook.com/{comment_id}"
driver.execute_script("window.open('');")
sleep(10)
driver.switch_to.window(driver.window_handles[1])
driver.get(url_2)
sleep(10)
driver.implicitly_wait(30)
names = driver.find_elements_by_class_name('_2b05')
names_text = [x.text for x in names]
comment_ids = driver.find_elements_by_xpath("//div[@class='_2b06']/div[@data-sigil='comment-body']")
for i,j in enumerate(comment_ids):
d[j.get_attribute('data-commentid')] = names_text[i]
driver.close()
driver.switch_to.window(driver.window_handles[0])
return d.get(comment_id,None)
with open('results2.csv','w',encoding='utf-8') as f:
writer = csv.writer(f,delimiter=',')
writer.writerow(["Facebook Post ID","Facebook Comment ID","Facebook Comment Content","Posted By","Created Date and Time"])
for i in comment_data:
if("comments" in i):
for j in i["comments"]['data']:
# print(i['id'],j['id'],j['message'],type(j['id']),j['created_time'])
writer.writerow([i['id'],j['id'],j['message'],get_name(j['id']),j['created_time']])
else:
writer.writerow([i['id'],"Na N","Na N","Na N"])
with open('data.json','w') as f:
json.dump(videos['videos']['data'],f)
df = pd.read_json('data.json')
df.to_csv("results.csv")
if __name__ == "__main__":
main()