-
Notifications
You must be signed in to change notification settings - Fork 3
/
books_scraper.py
272 lines (232 loc) · 9.77 KB
/
books_scraper.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import argparse
import requests
import bs4 as bs
from urllib.parse import quote
import re
from multiprocessing import cpu_count
from joblib import Parallel, delayed
import json
import os.path
import traceback
from termcolor import colored
from dotenv import load_dotenv
load_dotenv()
BASE_URL = "https://www.goodreads.com"
COOKIE = os.getenv("SESSION_ID")
# COOKIE = "_session_id2=83f123e4e12000a123f2bc6ff12da123"
PAGES_PER_SHELF = int(os.getenv("PAGES_PER_SHELF"))
# PAGES_PER_SHELF = 3
JOBS = cpu_count()
PROCESSING_RATIO_THRESHOLD = 1.0
def print_book(book):
print("-" * 30)
print(book["title"])
print(book["isbn"])
class GoodreadsScraper():
def __init__(self, skip_processed_shelves, use_saved_books, use_saved_books_urls):
self.skip_processed_shelves = skip_processed_shelves
self.use_saved_books = use_saved_books
self.use_saved_books_urls = use_saved_books_urls
self.shelves = []
self.shelves_stats = {}
def load_shelves(self):
with open("shelves.txt", "r") as f:
self.shelves = f.read().splitlines()
for year in range(1950, 2021):
self.shelves.append(str(year))
def scrap_shelves(self):
for i in range(1, PAGES_PER_SHELF + 1):
for shelf in self.shelves:
self.scrap_shelf(shelf, i)
def scrap_shelf(self, shelf, i):
print(colored("Started - {} - page {}".format(shelf, i), 'yellow', attrs=['bold']))
# Skip this shelf if already processed
if self.skip_processed_shelves \
and os.path.isfile("shelves_pages/{}_{}.json".format(shelf, i)) \
and self.shelf_processing_ratio(shelf, i) >= PROCESSING_RATIO_THRESHOLD:
print(colored("Finished - {} - page {} - already processed...".format(shelf, i), "magenta", attrs=['bold']))
return -1
# Get books urls (from disk or request)
shelf_books_urls_path = "shelves_pages_books_urls/{}_{}.json".format(shelf, i)
if self.use_saved_books_urls and os.path.isfile(shelf_books_urls_path):
with open(shelf_books_urls_path, "r", encoding='utf-8') as f:
books_urls = json.load(f)["books_urls"]
else:
shelf_url = BASE_URL + "/shelf/show/{}?page={}".format(shelf, i)
headers = {"Cookie": COOKIE}
try:
source = requests.get(shelf_url, timeout=5, headers=headers)
except Exception:
return 0
soup = bs.BeautifulSoup(source.content, features="html.parser")
books_urls = []
for elem in soup.find_all(class_="bookTitle"):
url = elem.get("href")
books_urls.append(BASE_URL + url)
with open(shelf_books_urls_path, "w") as f:
json.dump(
{"books_urls": books_urls},
f,
indent=4,
separators=(',', ': '),
ensure_ascii=False
)
# Scrap books urls
books = Parallel(n_jobs=JOBS, verbose=10)(delayed(self.scrap_book)(book_url) for book_url in books_urls)
books = list(filter(lambda x: x is not None, books))
with open("shelves_pages/{}_{}.json".format(shelf, i), "w") as f:
json.dump(
{"books": books},
f,
indent=4,
separators=(',', ': '),
sort_keys=True,
ensure_ascii=False
)
# Save shelf's stats
self.shelves_stats["{}_{}".format(shelf, i)] = {"scraped": len(books), "expected": len(books_urls)}
self.save_stats()
print(colored("Finished - {} - page {} with {}/{} books".format(shelf, i, len(books), len(books_urls)), 'green', attrs=['bold']))
return len(books)
def scrap_book(self, book_url):
try:
# Get book's source page (from disk or request)
book_source_page_path = "./books_source_pages/{}".format(quote(book_url, safe=""))
if self.use_saved_books and os.path.isfile(book_source_page_path):
soup_book = bs.BeautifulSoup(open(book_source_page_path), "html.parser")
else:
source_book = requests.get(book_url, timeout=5)
soup_book = bs.BeautifulSoup(source_book.content, features="html.parser")
with open(book_source_page_path, "w") as book_source_page:
book_source_page.write(str(soup_book))
try:
isbn = str(soup_book.find("meta", {"property": "books:isbn"}).get("content"))
except:
isbn = None
if isbn == "null":
isbn = None
metacol = soup_book.find(id="metacol")
title = metacol.find(id="bookTitle").text.strip()
author = metacol.find(class_="authorName").text.strip()
try:
description = metacol.find(id="description").find_all("span")[-1].text.strip()
except:
description = None
try:
img_url = soup_book.find(id="coverImage").get("src")
except:
img_url = None
try:
rating_count = int(metacol.find("meta", {"itemprop": "ratingCount"}).get("content"))
except:
rating_count = None
try:
rating_average = float(metacol.find("span", {"itemprop": "ratingValue"}).text)
except:
rating_average = None
try:
pages = int(soup_book.find("meta", {"property": "books:page_count"}).get("content"))
except:
pages = None
try:
details = metacol.find(id="details")
except:
details = None
try:
book_format = details.find("span", {"itemprop": "bookFormat"}).text
except:
book_format = None
try:
language = details.find("div", {"itemprop": "inLanguage"}).text
except:
language = None
try:
publication = details.find_all(class_="row")[1].text.strip()
except:
publication = None
try:
date_published = str(publication.split("\n")[1].strip())
except:
date_published = None
try:
publisher = publication.split("\n")[2].strip()[3:]
except:
publisher = None
try:
genres = soup_book.find_all(class_="actionLinkLite bookPageGenreLink")
genres = [genre.text for genre in genres]
except:
genres = []
try:
reviews_divs = soup_book.find_all(class_="reviewText stacked")
reviews = [review_div.find("span").find_all("span")[-1].text for review_div in reviews_divs]
except:
reviews = []
book = {
"title": title,
"author": author,
"description": description,
"img_url": img_url,
"isbn": isbn,
"rating_count": rating_count,
"rating_average": rating_average,
"date_published": date_published,
"publisher": publisher,
"genres": genres,
"book_format": book_format,
"pages": pages,
"language": language,
"goodreads_url": book_url,
"reviews": reviews
}
# print_book(book)
return book
except Exception as e:
print("-" * 30)
print(colored("ERROR: {}\n{}URL: {}".format(e, traceback.format_exc(), book_url), 'red'))
return
def load_stats(self):
with open("stats/shelves_stats.json", "r", encoding='utf-8') as f:
self.shelves_stats = json.load(f)
def save_stats(self):
with open("stats/shelves_stats.json", "w") as f:
json.dump(
self.shelves_stats,
f,
indent=4,
separators=(',', ': '),
sort_keys=True,
ensure_ascii=False
)
def shelf_processing_ratio(self, shelf, i):
shelf_stats = self.shelves_stats["{}_{}".format(shelf, i)]
try:
return shelf_stats["scraped"] / shelf_stats["expected"]
except ZeroDivisionError:
return 0
def check_cookie(self):
print(colored("Checking session cookie...", 'yellow', attrs=['bold']))
test_url = BASE_URL + "/shelf/show/mistery?page=2"
headers = {"Cookie": COOKIE}
source = requests.get(test_url, timeout=10, headers=headers)
soup = bs.BeautifulSoup(source.content, features="html.parser")
try:
validation_clue = soup.body.findAll(text=re.compile('Showing 1'), limit=1)
if len(validation_clue) > 0:
raise
except Exception:
raise Exception('Must use a valid session cookie!')
print(colored("Session cookie is OK!", 'green', attrs=['bold']))
def run(self):
self.check_cookie()
self.load_stats()
self.load_shelves()
self.scrap_shelves()
self.save_stats()
parser = argparse.ArgumentParser(description='Goodreads books scraper')
parser.add_argument('--skip_processed_shelves', action='store_true', help='Skip already processed shelves.')
parser.add_argument('--use_saved_books', action='store_true', help='Use saved books source files.')
parser.add_argument('--use_saved_books_urls', action='store_true', help='Use saved books urls.')
args = parser.parse_args()
scraper = GoodreadsScraper(args.skip_processed_shelves, args.use_saved_books, args.use_saved_books_urls)
scraper.run()