Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions domains/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import asyncio
import aiohttp
from bs4 import BeautifulSoup as BS
from fake_useragent import UserAgent
import json
import re

BASE_URL = "https://ovdi.ru/shop/malysham/"
HEADERS = {"User-Agent": UserAgent().random}

async def main():
products = []

async with aiohttp.ClientSession() as session:


async def main():
products = []
ua = UserAgent()

async with aiohttp.ClientSession() as session:
headers = {"User-Agent": ua.random}
async with session.get(BASE_URL, headers=headers) as response:
html = await response.text()
soup = BS(html, "html.parser")

items = soup.find_all("div", {"class": "bx_catalog_item_container"})
for item in items:
title_tag = item.find("div", {"class": "bx_catalog_item_title"})
title = title_tag.text.strip() if title_tag else "No title"

link_tag = title_tag.find("a") if title_tag else None
link = "https://ovdi.ru" + link_tag.get("href") if link_tag else None

price_tag = item.find("div", {"class": "bx_catalog_item_price"})
price = price_tag.text.strip() if price_tag else "No price"

barcode = "Штрихкод табылмады"
Comment thread
Rabkhat marked this conversation as resolved.
if link:
async with session.get(link, headers=HEADERS) as product_response:
product_html = await product_response.text()
product_soup = BS(product_html, "html.parser")

text = product_soup.get_text(" ", strip=True)
match = re.search(r"Штрихкод:\s*(\d+)", text)
if match:
barcode = match.group(1)

products.append({
"title": title,
"price": price,
product_infos.append({
"title": title,
"price": price,
"link": link
})

async def fetch_barcode(session, link):
if not link:
return "Штрихкод табылмады"
try:
async with session.get(link, headers=HEADERS) as product_response:
product_html = await product_response.text()
product_soup = BS(product_html, "html.parser")
text = product_soup.get_text(" ", strip=True)
match = re.search(r"Штрихкод:\s*(\d+)", text)
if match:
return match.group(1)
except Exception:
pass
return "Штрихкод табылмады"

tasks = [fetch_barcode(session, info["link"]) for info in product_infos]
barcodes = await asyncio.gather(*tasks)

for info, barcode in zip(product_infos, barcodes):
products.append({
"title": info["title"],
"price": info["price"],
"link": info["link"],
"barcode": barcode
})
Copy link

Copilot AI Aug 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No error handling for HTTP requests. Consider adding try-except blocks to handle potential network errors, timeouts, or HTTP error status codes.

Suggested change
})
try:
async with session.get(BASE_URL, headers=HEADERS) as response:
if response.status != 200:
print(f"Error: Failed to fetch main page. Status code: {response.status}")
return
html = await response.text()
soup = BS(html, "html.parser")
items = soup.find_all("div", {"class": "bx_catalog_item_container"})
for item in items:
title_tag = item.find("div", {"class": "bx_catalog_item_title"})
title = title_tag.text.strip() if title_tag else "No title"
link_tag = title_tag.find("a") if title_tag else None
link = "https://ovdi.ru" + link_tag.get("href") if link_tag else None
price_tag = item.find("div", {"class": "bx_catalog_item_price"})
price = price_tag.text.strip() if price_tag else "No price"
barcode = "Штрихкод табылмады"
if link:
try:
async with session.get(link, headers=HEADERS) as product_response:
if product_response.status != 200:
print(f"Warning: Failed to fetch product page {link}. Status code: {product_response.status}")
else:
product_html = await product_response.text()
product_soup = BS(product_html, "html.parser")
text = product_soup.get_text(" ", strip=True)
match = re.search(r"Штрихкод:\s*(\d+)", text)
if match:
barcode = match.group(1)
except (ClientError, TimeoutError) as e:
print(f"Warning: Exception occurred while fetching product page {link}: {e}")
products.append({
"title": title,
"price": price,
"link": link,
"barcode": barcode
})
except (ClientError, TimeoutError) as e:
print(f"Error: Exception occurred while fetching main page: {e}")
return

Copilot uses AI. Check for mistakes.

with open("products.json", "w", encoding="utf-8") as f:
json.dump(products, f, ensure_ascii=False, indent=4)

print("✅ products.json дайын!")
Copy link

Copilot AI Aug 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The success message is in a non-English language while the rest of the code uses English. Consider using consistent language throughout the codebase.

Suggested change
print("✅ products.json дайын!")
print("✅ products.json is ready!")

Copilot uses AI. Check for mistakes.

if __name__ == '__main__':
asyncio.run(main())