Spaces:
Sleeping
Sleeping
File size: 2,707 Bytes
422c1f3 567001c 422c1f3 567001c 422c1f3 567001c 422c1f3 567001c 422c1f3 567001c 422c1f3 567001c 422c1f3 567001c 422c1f3 567001c 422c1f3 567001c 422c1f3 567001c 422c1f3 567001c 422c1f3 567001c 422c1f3 567001c 422c1f3 567001c 7c3f29a | 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 | import requests
from bs4 import BeautifulSoup
import json
# --- FUNCTIONS ---
def sanitize_name(text):
"""Sanitizes text by removing Polish characters and special symbols for a folder name."""
polish_chars = {
"ą": "a",
"ć": "c",
"ę": "e",
"ł": "l",
"ń": "n",
"ó": "o",
"ś": "s",
"ź": "z",
"ż": "z",
}
text = text.lower()
result = ""
for char in text:
if char in polish_chars:
result += polish_chars[char]
elif char.isalnum():
result += char
else:
result += "_"
# Remove double underscores
while "__" in result:
result = result.replace("__", "_")
return result.strip("_")
def get_olx_data(url):
"""Fetches OLX offer data and returns it as a dictionary with title, description, parameters, and image URLs."""
# --- CONFIGURATION ---
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"
}
response = requests.get(url, headers=headers)
if response.status_code != 200:
return {"error": f"Connection error. Status code: {response.status_code}"}
soup = BeautifulSoup(response.content, "html.parser")
# --- GATHERING DATA ---
# Title
title_element = soup.find("h4", class_="css-1au435n")
title = title_element.get_text().strip() if title_element else "untitled"
# Description
description_element = soup.find("div", class_="css-19duwlz")
description = (
description_element.get_text(separator="\n").strip()
if description_element
else "No description"
)
# Parameters
parameter_list = []
parameters_container = soup.find(
"div", attrs={"data-testid": "ad-parameters-container"}
)
if parameters_container:
params = parameters_container.find_all("p", class_="css-13x8d99")
for p in params:
parameter_list.append(p.get_text().strip())
# Image Links
images = soup.select('img[data-testid^="swiper-image"]')
unique_links = list(set(img.get("src") for img in images if img.get("src")))
# --- RETURNING DICTIONARY ---
return {
"title": title,
"sanitized_title": sanitize_name(title),
"url": url,
"description": description,
"parameters": parameter_list,
"image_urls": unique_links,
"image_count": len(unique_links),
}
# --- USAGE ---
if __name__ == "__main__":
link = input("Enter the OLX offer link: ")
data = get_olx_data(link)
print(json.dumps(data, indent=4, ensure_ascii=False))
|