-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBid.py
More file actions
60 lines (50 loc) · 2.17 KB
/
Copy pathBid.py
File metadata and controls
60 lines (50 loc) · 2.17 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
from curl_cffi.requests import AsyncSession
import json
from bs4 import BeautifulSoup
import logging
logger = logging.getLogger(__name__)
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(name)s - %(message)s'
)
async def bid_img(vin):
scrape_ninja_url = "https://scrapeninja.p.rapidapi.com/scrape"
headers = {
"Content-Type": "application/json",
"X-Rapidapi-Key": "21ad582326mshd1131edf7f568aep14a7d8jsnf62d9fdf4b90"
}
payload = {
"url": f"https://bid.cars/app/search/en/vin-lot/{vin}/false",
"geo": "us"
}
try:
async with AsyncSession() as client:
response = await client.post(scrape_ninja_url, json=payload, headers=headers)
if response.status_code != 200:
logger.error(f"Ошибка: HTTP статус {response.status_code}")
return []
data = response.json()
body = json.loads(data.get("body", "{}"))
lot_url = body.get("url", None)
if not lot_url or "lot" not in lot_url:
logger.warning(f"URL для VIN {vin} на bid.cars не найден.")
return [], []
logger.info(f"URL страницы лота: {lot_url}")
lot_response = await client.get(lot_url, impersonate="edge101")
if lot_response.status_code != 200:
logger.error(f"Ошибка: Страница с лотом вернула HTTP код {lot_response.status_code}")
return [], lot_url
soup = BeautifulSoup(lot_response.text, "html.parser")
image_tags = soup.find_all("img")
image_urls = [
img["src"] for img in image_tags
if "src" in img.attrs and "bid.cars" in img["src"] and vin in img["src"]
]
if image_urls:
return image_urls, lot_url
else:
logger.warning(f"Изображения для VIN {vin} не найдены.")
return [], lot_url
except Exception as e:
logger.error(f"Ошибка при запросе: {e}")
return [], []