ScreenAvito_50_1

Делает принскрины по одной ссылке, например после фильтрации
 avatar
user_2065311
python
a month ago
1.7 kB
2
Indexable
Never
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from openpyxl import Workbook, load_workbook
from openpyxl.drawing.image import Image
import os

def get_ad_links(driver, url):
    driver.get(url)
    ad_elements = driver.find_elements(By.CSS_SELECTOR, "a[data-marker='item-title']")
    links = [element.get_attribute('href') for element in ad_elements]
    return links

def take_screenshot(url, driver, screenshot_path):
    driver.get(url)
    driver.save_screenshot(screenshot_path)

def insert_screenshot_to_excel(excel_path, screenshot_path, row):
    workbook = load_workbook(excel_path)
    sheet = workbook.active

    img = Image(screenshot_path)
    img.width, img.height = 123, 160  # Уменьшение размера изображения
    cell = f'C{row}'
    sheet.add_image(img, cell)

    workbook.save(excel_path)

def main(url, excel_path):
    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
    ad_links = get_ad_links(driver, url)
    row = 1
    for ad_link in ad_links:
        screenshot_path = f'screenshot_{row}.png'
        take_screenshot(ad_link, driver, screenshot_path)
        insert_screenshot_to_excel(excel_path, screenshot_path, row)
        row += 1

    driver.quit()

url = 'https://www.avito.ru/nizhniy_novgorod/kommercheskaya_nedvizhimost/prodam-ASgBAgICAUSwCNJW?context=H4sIAAAAAAAA_0q0MrSqLraysFJKK8rPDUhMT1WyLrYyNLNSKk5NLErOcMsvyg3PTElPLVGyrgUEAAD__xf8iH4tAAAA' #СМЕНИ ССЫЛКУ
excel_path = 'screenshots.xlsx' # файл для выгрузки результата
main(url, excel_path)