-
Notifications
You must be signed in to change notification settings - Fork 0
/
facebook.py
84 lines (64 loc) · 2.62 KB
/
facebook.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
## Print to screen the reviews from the given Facebook
## restaurant page, in the following format:
## Review's date - Reviewer's name => Rating value
# System dependencies
import os, sys, inspect
import time
import re
import csv
# Load Selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
# Load our Selenium library
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
sys.path.insert(0,currentdir+'/includes')
import common
# Webpage to crawl
url = input("Paste the URL: ") #https://www.facebook.com/pg/FreddoGelatoRoma/reviews/
# Main review element, containing all other elements (title, description, ratings...)
review_container_selector = "._1dwg"
# buttons
not_now = "expanding_cta_close_button"
# Elements to extract for each review (CSS selectors)
text_selector = "p"
reviewer_name_selector = ".profileLink"
rating_selector = "._51mq"
date_selector = "._5ptz"
page_name_selector = "._64-f span"
# Select driver (Chrome)
options = webdriver.ChromeOptions()
#options.add_argument('headless') # Uncomment to use headless browser
driver = webdriver.Chrome(chrome_options=options)
# Get HTML from URL and open cookies
driver.get(url)
# Wait 6 seconds
time.sleep(6)
# Get Title for CSV
page_name = driver.find_element_by_css_selector(page_name_selector).text
# Add CSV heading
common.face_setting_csv(page_name)
# Scroll Down
timer = time.time() + 15
while time.time() < timer:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
if driver.find_element_by_id(not_now).is_enabled():
driver.find_element_by_id(not_now).click()
# Get all review containers
review_container_elements = driver.find_elements_by_css_selector(review_container_selector)
# Loop - For each review container, extract its relevant elements
for review in review_container_elements:
# Get relevant review elements using CSS selectors
date = common.find_element_attribute_or_default( review, date_selector, 'title')
reviewer_name = common.find_element_text_or_default( review, reviewer_name_selector)
text = common.find_element_text_or_default( review, text_selector)
rating = common.find_element_text_or_default(review, rating_selector)
if len(rating) > 2:
rating_value = rating[:1] #5 stella to 5
# Uncomment to print reviews to screen
common.face_print_review(reviewer_name, date, rating_value, text)
# Uncomment to export results in csv file
common.face_export_review(reviewer_name, date, rating_value, text, page_name)
# Close driver
driver.close()