-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
43 lines (35 loc) · 1.62 KB
/
bot.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
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import csv
from selenium.common.exceptions import WebDriverException
csv_file = "pags.csv"
# Initialize the browser
driver = webdriver.Chrome()
try:
while True: # Infinite loop to keep cycling through the URLs
with open(csv_file, newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
url = row['url']
try:
# Visit the URL
driver.get(url)
print("Visiting:", url)
# Wait for the page to load completely
time.sleep(2)
# Gradually scroll down to the bottom of the page
total_height = driver.execute_script("return document.body.scrollHeight")
for i in range(0, total_height, 50): # Increment by 50 pixels
driver.execute_script(f"window.scrollTo(0, {i});")
time.sleep(0.05) # Sleep time between scrolls
# Scroll back up to the top of the page
driver.execute_script("window.scrollTo(document.body.scrollHeight, 0);")
# Wait a few seconds before moving to the next URL
time.sleep(4)
except WebDriverException as e:
print(f"Could not load URL {url}: {str(e)}")
continue # Move to the next URL
finally:
# Close the browser when done
driver.quit()