-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
148 lines (121 loc) · 5.51 KB
/
code.py
File metadata and controls
148 lines (121 loc) · 5.51 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
from playwright.sync_api import sync_playwright
import time
import random
import os
def scrape_dunnes_persistent(url):
with sync_playwright() as p:
# Use persistent context to save cookies and session
user_data_dir = os.path.join(os.getcwd(), 'browser_data')
os.makedirs(user_data_dir, exist_ok=True)
browser = p.chromium.launch_persistent_context(
user_data_dir=user_data_dir,
channel="chromium",
headless=True,
viewport={'width': 1920, 'height': 1080},
user_agent='Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
locale='en-IE',
timezone_id='Europe/Dublin',
geolocation={'longitude': -6.2603, 'latitude': 53.3498},
permissions=['geolocation'],
color_scheme='light',
args=[
'--disable-blink-features=AutomationControlled',
'--disable-dev-shm-usage',
],
extra_http_headers={
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8',
'Accept-Language': 'en-IE,en;q=0.9',
'Accept-Encoding': 'gzip, deflate, br',
'DNT': '1',
'Connection': 'keep-alive',
'Upgrade-Insecure-Requests': '1',
}
)
# Enhanced stealth script
browser.add_init_script("""
Object.defineProperty(navigator, 'webdriver', {
get: () => undefined
});
window.chrome = {
runtime: {},
loadTimes: function() {},
csi: function() {},
app: {}
};
Object.defineProperty(navigator, 'languages', {
get: () => ['en-IE', 'en']
});
Object.defineProperty(navigator, 'platform', {
get: () => 'MacIntel'
});
Object.defineProperty(navigator, 'hardwareConcurrency', {
get: () => 8
});
Object.defineProperty(navigator, 'deviceMemory', {
get: () => 8
});
""")
# Get the first page (persistent context creates one automatically)
pages = browser.pages
if pages:
page = pages[0]
else:
page = browser.new_page()
try:
print("Step 1: Establishing session with homepage (Persistent Context)...")
print("Note: This will save cookies for future runs")
page.goto("https://www.dunnesstoresgrocery.com", wait_until='domcontentloaded', timeout=30000)
time.sleep(random.uniform(5, 8))
# Check for error
if 'failed some security checks' in page.content():
print("Challenge detected, waiting...")
time.sleep(20)
page.reload(wait_until='networkidle', timeout=60000)
time.sleep(random.uniform(3, 5))
print(f"Step 2: Navigating to product page: {url}")
page.goto(url, wait_until='domcontentloaded', timeout=60000)
time.sleep(random.uniform(5, 8))
# Check for error
content_check = page.content()
if 'failed some security checks' in content_check or 'Error</title>' in content_check:
print("Challenge detected, waiting longer...")
time.sleep(25)
page.reload(wait_until='networkidle', timeout=60000)
time.sleep(random.uniform(5, 8))
content_check = page.content()
# Human-like behavior
for _ in range(random.randint(3, 5)):
page.mouse.move(random.randint(100, 1800), random.randint(100, 900))
time.sleep(random.uniform(1, 2))
for i in range(random.randint(2, 4)):
scroll_amount = random.randint(300, 600) * (i + 1)
page.evaluate(f"window.scrollTo(0, {scroll_amount})")
time.sleep(random.uniform(2, 3))
time.sleep(random.uniform(3, 5))
content = page.content()
print(f"Successfully fetched {len(content)} characters")
if 'failed some security checks' in content or 'Error</title>' in content:
print("⚠ Warning: Page may contain error message")
else:
print("✓ Session saved! Next run should be faster.")
return content
except Exception as e:
print(f"Error: {e}")
try:
page.screenshot(path='error_persistent.png')
print("Screenshot saved as error_persistent.png")
except:
pass
return None
finally:
browser.close()
if __name__ == "__main__":
url = "https://www.dunnesstoresgrocery.com/sm/delivery/rsid/258/categories/drinks/mixers-id-51042?f=Breadcrumb%3Agrocery%2Fdrinks%2Fmixers"
result = scrape_dunnes_persistent(url)
if result:
print("✓ Success!")
with open('dunnes_product_persistent.html', 'w', encoding='utf-8') as f:
f.write(result)
print("HTML saved to dunnes_product_persistent.html")
else:
print("✗ Failed")