This repository was archived by the owner on Apr 4, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
68 lines (60 loc) · 2.79 KB
/
Copy pathmain.py
File metadata and controls
68 lines (60 loc) · 2.79 KB
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
import asyncio
from playwright.async_api import async_playwright
import random
import argparse
# List of user agents to simulate different browsers
USER_AGENTS = [
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/92.0.4515.107 Safari/537.36',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) '
'Version/14.1.2 Safari/605.1.15',
'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:90.0) Gecko/20100101 Firefox/90.0',
]
# List of URLs to visit
async def simulate_user_interaction(page):
for _ in range(random.randint(2, 5)):
scroll_distance = random.randint(100, 1000)
await page.mouse.wheel(0, scroll_distance)
await asyncio.sleep(random.uniform(1, 3))
try:
selector = 'body *'
await page.wait_for_selector(selector, timeout=10000) # Increase timeout to 10 seconds
elements = await page.query_selector_all(selector)
if elements:
print(f"Found {len(elements)} elements to potentially hover over.")
element = random.choice(elements)
if element:
print("Hovering over an element.")
await element.hover(timeout=10000) # Increase timeout to 10 seconds
await asyncio.sleep(random.uniform(0.5, 1.5))
else:
print("No elements found for hovering.")
except Exception as e:
print(f"An error occurred during hover: {e}")
wait_time = random.randint(120, 180)
print(f"Staying on the page for {wait_time} seconds.")
await asyncio.sleep(wait_time)
async def visit_website(url, proxy=None):
async with async_playwright() as p:
launch_args = {'headless': True}
if proxy:
launch_args['proxy'] = {'server': proxy}
browser = await p.chromium.launch(**launch_args)
context = await browser.new_context(
viewport={'width': random.randint(800, 1920), 'height': random.randint(600, 1080)},
user_agent=random.choice(USER_AGENTS),
locale='en-US',
timezone_id='America/New_York',
)
page = await context.new_page()
await page.goto(url, wait_until='networkidle')
print(f"Visited {url} successfully using proxy {proxy if proxy else 'None'}!")
await simulate_user_interaction(page)
await browser.close()
if __name__ == "__main__":
# Parse command-line arguments
parser = argparse.ArgumentParser(description='Simulate user interaction on a website.')
parser.add_argument('-u', '--url', type=str, required=True, help='The URL of the website to visit')
parser.add_argument('-p', '--proxy', type=str, help='Proxy server (e.g., http://127.0.0.1:8080)')
args = parser.parse_args()
asyncio.run(visit_website(args.url, args.proxy))