-
Notifications
You must be signed in to change notification settings - Fork 1
/
scraper.py
53 lines (44 loc) · 1.52 KB
/
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
import requests
from bs4 import BeautifulSoup
Z_LIB = "https://b-ok.africa"
PDF_DRIVE = "https://www.pdfdrive.com"
def scrape_zlib(query):
response = []
url = f"{Z_LIB}/s/{query}"
page = requests.get(url)
soup = BeautifulSoup(page.content, "html.parser")
results = soup.find_all("div", class_="resItemBox resItemBoxBooks exactMatch")
for index, result in enumerate(results):
_ = {
"title": result.find("h3", {"itemprop": True}).contents[1].text.strip(),
"authors": ", ".join(
[_.text.strip() for _ in result.find_all("a", {"itemprop": True})]
),
"size": result.find("div", class_="bookProperty property__file")
.contents[-2]
.text.strip(),
"url": Z_LIB
+ result.find(
"div", class_="checkBookDownloaded itemCoverWrapper"
).contents[1]["href"],
}
# image validation
image = (
result.find("img", class_="cover")
.get("data-srcset")
.split(", ")[-1]
.split(" ")[0]
.strip()
if result.find("img", class_="cover").get("data-srcset")
else result.find("img", class_="cover")
.get("src")
.split(", ")[-1]
.split(" ")[0]
.strip()
)
if image.startswith("https://"):
_.update({"image": image})
else:
_.update({"image": Z_LIB + image})
response.append(_)
return response