-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper.py
More file actions
187 lines (151 loc) · 5.54 KB
/
scraper.py
File metadata and controls
187 lines (151 loc) · 5.54 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
"""
Finn.no scraper using ScrapingAnt API.
"""
import time
import re
from typing import List, Optional
import requests
from bs4 import BeautifulSoup
from config import (
SCRAPINGANT_API_URL,
SELECTORS,
DEFAULT_DELAY,
)
from models import CarListing, ListingCollection
from utils import (
build_search_url,
parse_listing_text,
extract_listing_id,
make_absolute_url,
clean_text
)
class FinnScraper:
"""Scraper for Finn.no car listings."""
def __init__(self, api_key: str, delay: float = DEFAULT_DELAY):
"""
Initialize the scraper.
Args:
api_key: ScrapingAnt API key
delay: Delay between requests in seconds
"""
self.api_key = api_key
self.delay = delay
self.collection = ListingCollection()
def _make_request(self, url: str) -> Optional[str]:
"""Make a request using ScrapingAnt API."""
params = {
"url": url,
"x-api-key": self.api_key,
"browser": "true"
}
try:
response = requests.get(SCRAPINGANT_API_URL, params=params, timeout=120)
response.raise_for_status()
return response.text
except requests.RequestException as e:
print(f"Request failed for {url}: {e}")
return None
def _parse_listings(self, html: str) -> List[CarListing]:
"""Parse listings from HTML content."""
soup = BeautifulSoup(html, 'html.parser')
listings = []
# Find all article elements
articles = soup.select(SELECTORS["listing_article"])
for article in articles:
try:
# Find the main link to listing details
link = article.select_one(SELECTORS["listing_link"])
if not link:
continue
href = link.get("href", "")
if "/mobility/item/" not in href:
continue
# Extract listing URL and ID
listing_url = make_absolute_url(href)
listing_id = extract_listing_id(href)
if not listing_id:
continue
# Get all text content from the article
article_text = article.get_text(separator="\n")
# Parse the text to extract fields
parsed = parse_listing_text(article_text)
# Get image
img = article.select_one("img")
image_url = img.get("src", "") if img else ""
listing = CarListing(
title=parsed.get("title", ""),
price=parsed.get("price", ""),
year=parsed.get("year", ""),
mileage=parsed.get("mileage", ""),
fuel_type=parsed.get("fuel_type", ""),
transmission=parsed.get("transmission", ""),
location=parsed.get("location", ""),
seller_name=parsed.get("seller_name", ""),
seller_type=parsed.get("seller_type", ""),
listing_id=listing_id,
listing_url=listing_url,
image_url=image_url,
time_posted=parsed.get("time_posted", "")
)
listings.append(listing)
except Exception as e:
print(f"Error parsing listing: {e}")
continue
return listings
def scrape(
self,
max_pages: int = 2,
min_price: Optional[int] = None,
max_price: Optional[int] = None,
min_year: Optional[int] = None,
max_year: Optional[int] = None,
fuel_type: Optional[str] = None
) -> int:
"""
Scrape car listings.
Args:
max_pages: Maximum number of pages to scrape
min_price: Minimum price filter (NOK)
max_price: Maximum price filter (NOK)
min_year: Minimum registration year filter
max_year: Maximum registration year filter
fuel_type: Fuel type filter
Returns:
Number of listings scraped
"""
total_scraped = 0
for page in range(1, max_pages + 1):
url = build_search_url(
page=page,
min_price=min_price,
max_price=max_price,
min_year=min_year,
max_year=max_year,
fuel_type=fuel_type
)
print(f"Scraping page {page}: {url}")
html = self._make_request(url)
if not html:
print(f"Failed to fetch page {page}")
continue
listings = self._parse_listings(html)
added = self.collection.add_many(listings)
print(f"Page {page}: scraped {len(listings)} listings, {added} new")
total_scraped += added
# Check if we got listings
if len(listings) == 0:
print("No more listings found, stopping pagination")
break
# Delay between requests
if page < max_pages:
time.sleep(self.delay)
return total_scraped
def get_results(self) -> ListingCollection:
"""Get the collection of scraped listings."""
return self.collection
def export_csv(self, filepath: str) -> None:
"""Export results to CSV."""
self.collection.to_csv(filepath)
def export_json(self, filepath: str) -> None:
"""Export results to JSON."""
self.collection.to_json(filepath)