-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoodReads_Review_Scraping_Only_V2_No_Images.py
146 lines (90 loc) · 4.3 KB
/
GoodReads_Review_Scraping_Only_V2_No_Images.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python
# coding: utf-8
# In[1]:
from selenium import webdriver
from PIL import Image
from io import BytesIO
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.keys import Keys
import time
# In[2]:
import argparse
parser = argparse.ArgumentParser(description='Supplying urls')
parser.add_argument("-url", "--goodreads_url", help="Determines the url used.", required = True)
args = parser.parse_args()
print(args.goodreads_url)
GoodReadsurl = r'https://www.goodreads.com/book/show/7303702-street-fighting-mathematics'
GoodReadsurl = args.goodreads_url
# In[3]:
#GoodReadsurl = r'https://www.goodreads.com/book/show/33237408-execution'
driver = webdriver.Chrome('chromedriver.exe')
driver.get(GoodReadsurl)
import requests
from bs4 import BeautifulSoup
import pandas as pd
goodreads = driver.page_source
soup=BeautifulSoup(goodreads) #prepare it for beautiful soup
reviewcontent = []
GRreviewer = []
StarRating = []
ReviewDate = []
GRreviews = 0
goodreads = driver.page_source
soup=BeautifulSoup(goodreads) #prepare it for beautiful soup
time.sleep(8)
# In[4]:
try:
link = driver.find_element_by_xpath('//img[contains(@src, "//s.gr-assets.com/assets/gr/icons/icon_close-63734f04e7baaa77fbad796225e5724c.svg")]')
link.click()
except: print('no pop up')
# In[5]:
for j in soup.select('span[id*="reviewTextCont"]')[0:len(soup.select('a[class*="reviewDate createdAt right"]'))]:#pull out the information from speific elements using these attributes
reviewcontent.append(j.get_text())
GRreviews += 1
for k in soup.select('a[class="user"]')[0:len(soup.select('a[class*="reviewDate createdAt right"]'))]:
GRreviewer.append(k.get_text())
for m in soup.select('span[class="staticStars notranslate"]')[0:len(soup.select('a[class*="reviewDate createdAt right"]'))]:
StarRating.append(m.get_text())
for n in soup.select('a[class*="reviewDate createdAt right"]')[0:30]:
ReviewDate.append(n.get_text())
pages = len(driver.find_elements_by_xpath('//a[contains(@onclick, "page=")]'))
print()
for i in range(2,(pages-1),1):
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
link = driver.find_element_by_xpath('//a[contains(@onclick, "page='+str(i)+'")]')# this is it!!!!! just change page number and trunk of url
link.click()
time.sleep(3)
goodreads = driver.page_source
soup=BeautifulSoup(goodreads) #prepare it for beautiful soup
# In[6]:
j=0
for j in soup.select('span[id*="reviewTextCont"]')[0:len(soup.select('a[class*="reviewDate createdAt right"]'))]:#pull out the information from speific elements using these attributes
reviewcontent.append(j.get_text())
GRreviews += 1
print('Reviews = '+str(len(soup.select('span[id*="reviewTextCont"]'))))
for k in soup.select('a[class="user"]')[0:len(soup.select('a[class*="reviewDate createdAt right"]'))]:
GRreviewer.append(k.get_text())
#print(k.get_text)
print('Reviewers = '+str(len(soup.select('a[class*="user"]'))))
for m in soup.select('span[class="staticStars notranslate"]')[0:len(soup.select('a[class*="reviewDate createdAt right"]'))]:
StarRating.append(m.get_text())
#print(m.get_text)
#print('hello')
print('Ratings = '+str(len(soup.select('span[class*=" staticStars notranslate"]'))))
for n in soup.select('a[class*="reviewDate createdAt right"]')[0:30]:
#print(n.get_text)
ReviewDate.append(n.get_text())
print('Dates = '+str(len(soup.select('a[class*="reviewDate createdAt right"]'))))
driver.quit()
GRReviews=pd.DataFrame(columns=['Review']) #initialise dataframez
GRReviews['Review']=pd.Series(reviewcontent) #fill reviews column
GRReviews['rating']=pd.Series(StarRating) #fill ratings column
GRReviews['Reviewer'] = pd.Series(GRreviewer) #fill reviewers columns
GRReviews['Review Date'] = pd.Series(ReviewDate)#fill reviewers columns
# In[7]:
GRReviews.to_csv('GRReviews.csv')
# In[ ]: