-
Notifications
You must be signed in to change notification settings - Fork 176
Expand file tree
/
Copy pathebay_related_searches.py
More file actions
596 lines (486 loc) · 20.1 KB
/
Copy pathebay_related_searches.py
File metadata and controls
596 lines (486 loc) · 20.1 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
####################################################################################
# Author : Lee Foot #
# Website : https://www.leefoot.com #
# Contact : https://www.leefoot.com/contact #
# Email : hello@leefoot.com #
# LinkedIn : https://www.linkedin.com/in/lee-foot/ #
# Bluesky : https://bsky.app/profile/leefootseo.bsky.social #
####################################################################################
import streamlit as st
import re
import json
import time
import random
st.set_page_config(
page_title="eBay Related Search Scraper | LeeFootSEO",
page_icon="🔍",
layout="wide",
initial_sidebar_state="collapsed",
)
from streamlit_echarts import st_echarts
from stqdm import stqdm
import pandas as pd
from bs4 import BeautifulSoup
import requests
from user_agent2 import generate_user_agent
# ============================================================================
# INITIALIZE SESSION STATE
# ============================================================================
if 'df' not in st.session_state:
st.session_state.df = None
if 'seed_keyword' not in st.session_state:
st.session_state.seed_keyword = None
if 'scraping_complete' not in st.session_state:
st.session_state.scraping_complete = False
# ============================================================================
# HELPER FUNCTIONS
# ============================================================================
def get_headers():
"""Generate fresh headers with a new user agent for each request."""
ua = generate_user_agent(navigator="chrome")
return {
'User-Agent': str(ua),
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8',
'Accept-Language': 'en-GB,en-US;q=0.9,en;q=0.8',
'Accept-Encoding': 'gzip, deflate, br',
'Connection': 'keep-alive',
'Upgrade-Insecure-Requests': '1',
'Sec-Fetch-Dest': 'document',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-Site': 'none',
'Sec-Fetch-User': '?1',
'Cache-Control': 'max-age=0',
}
def is_blocked(response_text):
"""Check if eBay has blocked the request with a CAPTCHA page."""
blocked_indicators = [
'Pardon our interruption',
'please verify yourself',
'unusual traffic',
'captcha',
'security measure',
]
response_lower = response_text.lower()
return any(indicator.lower() in response_lower for indicator in blocked_indicators)
def make_request(url, session):
"""Make a request with retry logic and delay."""
max_retries = 2
for attempt in range(max_retries):
try:
if attempt > 0:
time.sleep(random.uniform(2, 4))
response = session.get(url, headers=get_headers(), timeout=15)
if response.status_code == 200 and not is_blocked(response.text):
return response
elif is_blocked(response.text):
if attempt < max_retries - 1:
time.sleep(random.uniform(3, 5))
continue
return None
except requests.exceptions.RequestException:
if attempt < max_retries - 1:
continue
return None
return None
RELATED_SELECTORS = [
'.srp-related-searches a',
'.s-answer-region-above-river a',
'[data-testid="related-searches"] a',
'.srp-river-answer--RELATED_SEARCHES a',
'.b-visualnav__links a',
'section.b-module a[href*="_nkw="]',
]
URL_PATH = "/sch/i.html?_nkw="
def extract_keyword_from_url(href):
"""Extract and decode keyword from eBay URL parameter."""
match = re.search(r'_nkw=([^&]+)', href)
if match:
kw = match.group(1)
kw = kw.replace('+', ' ')
kw = requests.utils.unquote(kw)
return kw.strip()
return None
def extract_related_searches(soup):
"""Try multiple methods to extract related searches from eBay."""
related_kws = []
for selector in RELATED_SELECTORS:
elements = soup.select(selector)
if elements:
for el in elements:
href = el.get('href', '')
if '_nkw=' in href or 'sch/i.html' in href:
kw = extract_keyword_from_url(href)
if kw and len(kw) > 1 and kw not in related_kws:
related_kws.append(kw)
if related_kws:
return related_kws
all_links = soup.find_all('a', href=True)
for link in all_links:
href = link.get('href', '')
if '_nkw=' in href and 'sch/' in href:
kw = extract_keyword_from_url(href)
if kw and len(kw) > 1 and kw not in related_kws:
link_text = link.get_text(strip=True).lower()
if link_text not in ['shop by category', 'home', 'ebay']:
related_kws.append(kw)
scripts = soup.find_all('script', type='application/json')
for script in scripts:
try:
data = json.loads(script.string)
if isinstance(data, dict):
for key in ['relatedSearches', 'related_searches', 'suggestions']:
if key in data:
items = data[key]
if isinstance(items, list):
for item in items:
if isinstance(item, str):
related_kws.append(item)
elif isinstance(item, dict) and 'keyword' in item:
related_kws.append(item['keyword'])
except (json.JSONDecodeError, TypeError):
continue
return list(set(related_kws))
def get_ebay_url(cctld, keyword):
"""Build eBay search URL"""
base = f"https://www.ebay{cctld}{URL_PATH}"
return base + requests.utils.quote(keyword)
# ============================================================================
# UI
# ============================================================================
st.title("🔍 eBay Related Search Scraper")
st.markdown("*Created by* [](https://www.leefoot.com) · [](https://www.leefoot.com/contact) · [](https://www.linkedin.com/in/lee-foot/) · [](https://bsky.app/profile/leefootseo.bsky.social) · [](https://leefoot.com/tools) · [](https://github.com/searchsolved/search-solved-public-seo)")
with st.expander("How to use this tool"):
st.markdown("""
**What this tool does:**
- Extracts related searches from eBay
- Discovers product keyword variations
- Builds comprehensive keyword lists
**How to use:**
1. Enter seed keywords
2. Configure search depth
3. Extract related searches
4. Download keyword list
**Best for:**
- Ecommerce keyword research
- Product title optimization
- Long-tail keyword discovery
""")
st.markdown("Discover keyword opportunities by mapping eBay's related search suggestions into an interactive visualization.")
st.markdown("---")
# Sidebar for settings
with st.sidebar:
st.header("⚙️ Settings")
cctld_options = {
'🇬🇧 United Kingdom': '.co.uk',
'🇺🇸 United States': '.com',
'🇩🇪 Germany': '.de',
'🇪🇸 Spain': '.es',
'🇫🇷 France': '.fr',
'🇳🇱 Netherlands': '.nl',
'🇦🇺 Australia': '.com.au',
'🇨🇦 Canada': '.ca',
'🇮🇹 Italy': '.it',
}
selected_country = st.selectbox(
'eBay Marketplace',
options=list(cctld_options.keys()),
help='Select which eBay marketplace to search'
)
ccTLD = cctld_options[selected_country]
st.markdown("---")
st.subheader("ℹ️ How It Works")
st.markdown("""
1. **Search** - Enter a seed keyword
2. **Expand** - We find related searches on eBay
3. **Go Deeper** - Each related search is expanded
4. **Visualize** - Results shown as an interactive tree
""")
st.markdown("---")
st.subheader("⚠️ Note")
st.caption("eBay may occasionally block requests. If this happens, wait a few minutes and try again with a different keyword.")
# Main content
col1, col2 = st.columns([1, 2])
with col1:
with st.form(key='search_form'):
seed_keyword_input = st.text_input(
'🔎 Seed Keyword',
placeholder='e.g., running shoes',
help='Enter the keyword you want to explore'
)
submitted = st.form_submit_button('🚀 Start Scraping', use_container_width=True)
with col2:
if not submitted and not st.session_state.scraping_complete:
st.info("👈 Enter a keyword and click **Start Scraping** to begin")
# ============================================================================
# MAIN SCRAPING LOGIC
# ============================================================================
if submitted:
if not seed_keyword_input.strip():
st.error("⚠️ Please enter a keyword to search")
st.stop()
# Store the seed keyword in session state
st.session_state.seed_keyword = seed_keyword_input.strip()
seed_keyword = st.session_state.seed_keyword
# Progress section
progress_container = st.container()
with progress_container:
status_text = st.empty()
progress_bar = st.progress(0)
related_search_kws = []
source_kws = []
final_kws = []
session = requests.Session()
# First request
status_text.text("🔍 Searching eBay for related keywords...")
progress_bar.progress(10)
search_url = get_ebay_url(ccTLD, seed_keyword)
response = make_request(search_url, session)
if response is None:
progress_container.empty()
st.error("⚠️ eBay blocked the request (CAPTCHA/bot detection)")
st.info("""
**Tips to try:**
- Wait a few minutes and try again
- Try a different keyword
- Try a different marketplace (in sidebar)
""")
st.stop()
soup = BeautifulSoup(response.text, "html.parser")
related_search_kws = extract_related_searches(soup)
if not related_search_kws:
progress_container.empty()
st.warning("😕 No related searches found for this keyword")
st.info("Try a broader or different keyword.")
with st.expander("🔧 Debug Information"):
st.code(f"URL: {search_url}")
st.code(f"Status: {response.status_code}")
st.text_area("Response HTML (first 3000 chars)", response.text[:3000], height=200)
st.stop()
progress_bar.progress(25)
status_text.text(f"✅ Found {len(related_search_kws)} related searches! Expanding...")
# Second loop
blocked_count = 0
total_kws = len(related_search_kws)
for idx, kw in enumerate(related_search_kws):
time.sleep(random.uniform(1.5, 3.0))
progress_pct = 25 + int((idx / total_kws) * 65)
progress_bar.progress(progress_pct)
status_text.text(f"🔄 Processing: {kw} ({idx + 1}/{total_kws})")
search_url = get_ebay_url(ccTLD, kw)
response = make_request(search_url, session)
if response is None:
blocked_count += 1
if blocked_count >= 3:
st.warning("⚠️ Multiple requests blocked. Stopping early.")
break
continue
soup_lv2 = BeautifulSoup(response.text, "html.parser")
lv2_related = extract_related_searches(soup_lv2)
for lv2_kw in lv2_related:
source_kws.append(kw)
final_kws.append(lv2_kw)
session.close()
progress_bar.progress(100)
status_text.text("✅ Complete!")
time.sleep(0.5)
progress_container.empty()
# Build dataframe
if not source_kws:
df = pd.DataFrame({
'seed_keyword': [seed_keyword] * len(related_search_kws),
'related_searches': related_search_kws
})
else:
df = pd.DataFrame({
'seed_keyword': source_kws,
'related_searches': final_kws
})
df = df.drop_duplicates().reset_index(drop=True)
# Store in session state
st.session_state.df = df
st.session_state.scraping_complete = True
# ============================================================================
# DISPLAY RESULTS (from session state)
# ============================================================================
if st.session_state.scraping_complete and st.session_state.df is not None:
df = st.session_state.df
seed_keyword = st.session_state.seed_keyword
# Results section
st.markdown("---")
st.subheader(f"📊 Results for: {seed_keyword}")
# Stats
unique_l1 = df['seed_keyword'].nunique()
unique_l2 = df['related_searches'].nunique()
total_relationships = len(df)
col_stat1, col_stat2, col_stat3 = st.columns(3)
with col_stat1:
st.metric("Level 1 Keywords", unique_l1)
with col_stat2:
st.metric("Level 2 Keywords", unique_l2)
with col_stat3:
st.metric("Total Relationships", total_relationships)
# Download and data
st.markdown("")
col_dl, col_spacer = st.columns([1, 2])
with col_dl:
csv = df.to_csv(index=False).encode('utf-8')
st.download_button(
label="📥 Download CSV",
data=csv,
file_name=f'ebay_related_{seed_keyword.replace(" ", "_")}.csv',
mime='text/csv',
use_container_width=True
)
with st.expander("📋 View Data Table"):
st.dataframe(df, use_container_width=True, height=300)
# Visualization
st.markdown("---")
st.subheader("🌳 Keyword Relationship Tree")
# Build tree data
children_list = []
for int_word in df['seed_keyword'].unique():
children_list_level_2 = []
for query_2 in df[df['seed_keyword'] == int_word]['related_searches'].unique():
children_list_level_2.append({"name": query_2})
children_list.append({'name': int_word, 'children': children_list_level_2})
tree = {'name': seed_keyword, 'children': children_list}
# View selector
view_type = st.radio(
"Select view",
["🔵 Radial Tree", "📊 Vertical Tree", "📁 Text Tree"],
horizontal=True,
help="Choose how to visualize the keyword relationships"
)
if view_type == "🔵 Radial Tree":
st.caption("Click nodes to expand/collapse. Right-click to save as image.")
opts = {
"tooltip": {
"trigger": "item",
"triggerOn": "mousemove",
},
"series": [
{
"type": "tree",
"data": [tree],
"layout": "radial",
"top": "2%",
"left": "2%",
"bottom": "2%",
"right": "2%",
"symbolSize": 10,
"symbol": "circle",
"itemStyle": {
"color": "#10B981",
"borderColor": "#059669",
"borderWidth": 2,
},
"lineStyle": {
"color": "#94A3B8",
"width": 1.5,
"curveness": 0.5,
},
"label": {
"fontSize": 11,
},
"emphasis": {
"itemStyle": {
"color": "#F59E0B",
"borderColor": "#D97706",
},
"lineStyle": {
"color": "#F59E0B",
"width": 2,
},
},
"expandAndCollapse": True,
"initialTreeDepth": 2,
"animationDuration": 550,
"animationDurationUpdate": 750,
}
],
}
st_echarts(opts, key=f"radial_tree_{seed_keyword}", height=800)
elif view_type == "📊 Vertical Tree":
st.caption("Click nodes to expand/collapse. Right-click to save as image.")
opts = {
"tooltip": {
"trigger": "item",
"triggerOn": "mousemove",
},
"series": [
{
"type": "tree",
"data": [tree],
"layout": "orthogonal",
"orient": "LR",
"top": "5%",
"left": "10%",
"bottom": "5%",
"right": "20%",
"symbolSize": 8,
"symbol": "circle",
"itemStyle": {
"color": "#3B82F6",
"borderColor": "#2563EB",
"borderWidth": 2,
},
"lineStyle": {
"color": "#94A3B8",
"width": 1.5,
},
"label": {
"position": "right",
"fontSize": 11,
"verticalAlign": "middle",
},
"leaves": {
"label": {
"position": "right",
"verticalAlign": "middle",
}
},
"emphasis": {
"itemStyle": {
"color": "#F59E0B",
"borderColor": "#D97706",
},
"lineStyle": {
"color": "#F59E0B",
"width": 2,
},
},
"expandAndCollapse": True,
"initialTreeDepth": 2,
"animationDuration": 550,
"animationDurationUpdate": 750,
}
],
}
# Calculate height based on number of nodes
tree_height = max(600, unique_l1 * 50 + unique_l2 * 15)
st_echarts(opts, key=f"vertical_tree_{seed_keyword}", height=tree_height)
else: # Text Tree
st.caption("Click on folders to expand/collapse.")
st.markdown("")
# Build text tree with expanders
st.markdown(f"**🌱 {seed_keyword}**")
for l1_keyword in df['seed_keyword'].unique():
l2_keywords = df[df['seed_keyword'] == l1_keyword]['related_searches'].unique().tolist()
with st.expander(f"📂 {l1_keyword} ({len(l2_keywords)} keywords)"):
for l2_kw in l2_keywords:
st.write(f"└── 📄 {l2_kw}")
# Clear results button
st.markdown("---")
if st.button("🔄 Start New Search", use_container_width=False):
st.session_state.df = None
st.session_state.seed_keyword = None
st.session_state.scraping_complete = False
st.rerun()
# Footer
st.markdown("---")
st.markdown(
"Made by 🌐 [Lee Foot](https://www.leefoot.com) · "
"[🦋 Bluesky](https://bsky.app/profile/leefootseo.bsky.social) · "
"[LinkedIn](https://www.linkedin.com/in/lee-foot/) · "
"[📧 Contact](https://www.leefoot.com/contact)"
)