adierebel / Open wattpad in selenium

22 Nov 2019 at 08:12

1 from selenium import webdriver
2 from selenium.webdriver.firefox.options import Options
3 from sys import argv, path as sys_path
4 import time
5
6 # Total
7 total_loop = 1
8
9 def start_browser(url, headless=False):
10 global total_loop
11 delay_time = 5
12 web_driver_path = "./driver/geckodriver"
13 target_url = url
14
15 options = Options()
16 options.headless = headless
17
18 # Logs
19 print()
20 print(" *"+("*" * 60 ))
21 print(" * Wattpad Floody")
22 print(" * Headless:", headless)
23 print(" * Target:", url)
24 print(" * Delay:", delay_time, "secs")
25 print(" *"+("*" * 60 ))
26
27 # Init webdriver
28 browser = webdriver.Firefox(options=options, executable_path=web_driver_path)
29
30 try:
31 # Loop forever
32 while True:
33 # Logs
34 print(" * Tries:", total_loop)
35
36 # Open browser
37 browser.get(target_url)
38
39 # Get scroll height
40 last_height = browser.execute_script("return document.body.scrollHeight")
41
42 while True:
43 # Scroll down to bottom
44 browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
45
46 # Wait to load page
47 time.sleep(0.5)
48
49 # Calculate new scroll height and compare with last scroll height
50 new_height = browser.execute_script("return document.body.scrollHeight")
51 if new_height == last_height:
52 break
53 last_height = new_height
54
55 # Delay
56 time.sleep(delay_time)
57
58 # Quit
59 total_loop += 1
60
61 except KeyboardInterrupt:
62 print(" * Exit...")
63
64 except Exception as e:
65 print(" * Error:", e)
66 print(" * Restarting...")
67 if target_url:
68 start_browser(target_url)
69 else:
70 print(" * URL Gone, exiting...")
71
72 # Start
73 if len(argv) == 2:
74 target_url = str(argv[1])
75 start_browser(target_url)
76 elif len(argv) == 3:
77 target_url = str(argv[1])
78 headless = str(argv[2]) == "--headless"
79 start_browser(target_url, headless)
80 else:
81 print(" * Please provide URL to start...")

Comments (0)