-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathebay_sync.py
More file actions
139 lines (80 loc) · 3.35 KB
/
Copy pathebay_sync.py
File metadata and controls
139 lines (80 loc) · 3.35 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
import requests
from parsel import Selector
from urllib.parse import urlencode, parse_qs, urlparse
import csv
import re
SCRAPERAPI_KEY = "YOUR_SCRAPERAPI_KEY"
TARGET_URL = "https://www.ebay.co.uk/b/bn_7117580375?_sop=10&mag=1&rt=nc"
OUTPUT_FILE = "SAMPLE_UK_PS5_10_Items.csv"
def extract_item_id(url):
"""Extracts eBay item ID from both standard (/itm/) and catalog (/p/?iid=) URLs."""
if not url:
return "N/A"
itm_match = re.search(r"/itm/(\d+)", url)
if itm_match:
return itm_match.group(1)
parsed_url = urlparse(url)
query_params = parse_qs(parsed_url.query)
if "iid" in query_params:
return query_params["iid"][0]
return "N/A"
def scrape_ebay():
print("[-->] Fetching search page via ScraperAPI...")
payload = {"api_key": SCRAPERAPI_KEY, "url": TARGET_URL, "render": "true"}
response = requests.get("https://api.scraperapi.com/", params=payload)
if response.status_code != 200:
print(f"❌ ScraperAPI Request Failed: {response.status_code}")
return
selector = Selector(text=response.text)
items = selector.css(
"li.brwrvr__item-card, li.bsig__item, ul.b-list__items_nofooter > li, li.s-item"
)
print(f"[-->] Found {len(items)} item cards matching layout containers.")
scraped_data = []
seen_urls = set()
for item in items:
if len(scraped_data) >= 10:
break
url = item.css(
"a.bsig__title__wrapper::attr(href), a.s-item__link::attr(href)"
).get()
if not url or url in seen_urls:
continue
title_parts = item.css(
"h3.bsig__title__text ::text, .s-item__title ::text"
).getall()
full_title = " ".join([t.strip() for t in title_parts if t.strip()])
clean_title = full_title.replace("New listing", "").strip()
if not clean_title or "Shop on eBay" in clean_title:
continue
condition = item.css(
".bsig__listingCondition span::text, .s-item__subtitle ::text"
).get()
clean_condition = condition.strip() if condition else "N/A"
price = item.css(".bsig__price--displayprice::text, .s-item__price::text").get()
clean_price = price.strip() if price else "N/A"
item_id = extract_item_id(url)
seen_urls.add(url)
scraped_data.append(
{
"ebay_item_number": item_id,
"title": clean_title,
"condition": clean_condition,
"price": clean_price,
"url": url,
}
)
print(
f"[{len(scraped_data)}/10] ID: {item_id} | Price: {clean_price} | Cond: {clean_condition} | Title: {clean_title[:30]}..."
)
# Export to CSV with UTF-8 BOM encoding for Excel compatibility
if scraped_data:
with open(OUTPUT_FILE, "w", newline="", encoding="utf-8-sig") as f:
writer = csv.DictWriter(f, fieldnames=scraped_data[0].keys())
writer.writeheader()
writer.writerows(scraped_data)
print(f"\n✅ SUCCESS: Clean UTF-8 CSV saved to '{OUTPUT_FILE}'")
else:
print("\n❌ No items extracted.")
if __name__ == "__main__":
scrape_ebay()