forked from AhmetAksunger/ZLibrary_BookSearcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
book_searcher.py
99 lines (74 loc) · 2.94 KB
/
book_searcher.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
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
#Enter your zlibrary email and password
email = ''
password = ''
url = 'https://singlelogin.me/'
chrome_options = Options()
chrome_options.add_argument('--headless')
driver = webdriver.Chrome(options=chrome_options)
def wait_until(locator: tuple):
WebDriverWait(driver,10).until(EC.visibility_of_element_located(locator))
def get_webpage(url):
driver.get(url)
def get_login_fields():
wait_until((By.NAME,'email'))
email_field = driver.find_element(By.NAME,'email')
wait_until((By.NAME,'email'))
password_field = driver.find_element(By.NAME,'password')
return email_field, password_field
def send_login_keys(email_field,password_field,email,password):
email_field.send_keys(email)
password_field.send_keys(password)
password_field.send_keys(Keys.RETURN)
WebDriverWait(driver,10).until(EC.url_changes(url))
def main_login(url,email,password):
get_webpage(url)
email_field,password_field = get_login_fields()
send_login_keys(email_field,password_field,email,password)
def get_search_field():
wait_until((By.XPATH,'//*[@id="searchFieldx"]'))
search_field = driver.find_elements(By.NAME,'q')
return search_field
def send_search_keys(search_field,search_input):
search_field[1].clear()
search_field[1].send_keys(search_input)
search_field[1].send_keys(Keys.RETURN)
def get_books():
wait_until((By.XPATH,'//h3[@itemprop="name"]/a'))
book_titles = driver.find_elements(By.XPATH,'//h3[@itemprop="name"]/a')
count = 0
books = {}
links = {}
for title in book_titles[:10]:
count += 1
print(f"{count}-) {title.text}")
books[count] = title.text
links[count] = title.get_attribute('href')
return books,links
def main_search():
search_input = input("Enter a book name: ")
while True:
search_field = get_search_field()
send_search_keys(search_field,search_input)
books,links = get_books()
while True:
index = input("(s: search another book, q: quit)\nEnter a book index: ")
if index.isdigit():
index = int(index)
print("----------------------------------------------")
print(books.get(index,"Enter a valid index"))
print(links.get(index,"-"))
print("----------------------------------------------")
elif index == 'q':
return
elif index == 's':
search_input = input("Enter a book name: ")
break
main_login(url,email,password)
main_search()
driver.close()