@Aibot92

Как достать ссылку на изображение?

Доброго дня
есть написаный парсинг :
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
import csv
import os

URL = 'https://www.svyaznoy.ru/catalog/phone/8605/'
HEDARS = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Safari/605.1.15', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'}
FILE = 'svz.csv'
direct= os.getcwd()

def get_himl(url):
    chromedriver =direct+'/chromedriver'
    options = webdriver.ChromeOptions()
    browser = webdriver.Chrome(executable_path=chromedriver,chrome_options=options)
    browser.get(url)
    generated_html = browser.page_source
    browser.quit()
    return generated_html

def seve_file(item,path):
    with open(path, 'w', newline='',encoding='utf-8') as file:
        writer = csv.writer(file, delimiter = ';')
        writer.writerow(['модель', 'цена акция (без учета комбо)', 'акции', 'старая цена/цена без акции'])
        for items in item:
            writer.writerow([items['title'], items['prise'], items['action'], items['old_prise']])


def get_content(html):
    soup = BeautifulSoup(html, 'html.parser')
    items = soup.find_all('div', {'class': ["b-product-block__content"]})

    phone =[]
    p = len(items)
    for items in items:
        prise = items.find('span', class_="b-product-block__visible-price")
        if prise:
            prise = prise.get_text()
            prise = prise.replace('\xa0', ' ')
        else:
            prise = "цена не указана"
        sale = items.find('div', class_="b-product-block__action-info _old-price")
        if sale:
            sale = sale.get_text()
        else:
            sale = ""
        actions = items.find('div', class_="b-product-block__action-info _action-price")
        if actions:
            actions = actions.get_text()
        else:
            actions = ""
        discont = items.find('span', class_="discount-sum")
        if discont:
            discont = discont.get_text()
            discont = "скидка по комбо: " + discont
        else:
            discont = ""
        old_prise = items.find('s', class_="b-product-block__price-old")
        if old_prise:
            old_prise = old_prise.get_text()
            old_prise = old_prise.replace('\xa0', ' ')
        else:
            old_prise = ""

        oll_action = sale + " " + actions + " " + discont
        if oll_action:
            oll_action = oll_action
        else:
            oll_action = ''
        pic_href = items.find('img', class_="lazy").get('content')
        print(pic_href)
        phone.append({
        'title' : items.find('span', class_="b-product-block__name").get_text(),
        'prise' : prise.replace('руб.', ' '),
        'action' : oll_action,
        'old_prise' : old_prise.replace('руб.', ' ')

        })
    return phone
def parsing():
    phone = []
    for page in range (1,3):
        print(f'Анализ {page}  ...')
        a = URL + 'page-'+ str(page)
        html = get_himl(a)
        phone.extend(get_content(html))
        seve_file(phone,FILE)
    print(f'найдено ' + str(len(phone)) + ' телефонов')
parsing()


к нему захотел добавить извлечение картинок но при поиске ссылки выдает ошибку :
pic_href = items.find('img', class_="lazy").get('content')
        print(pic_href)


File "/Users/alexs/Desktop/py/Parsing/parser_svz.py", line 70, in get_content
pic_href = items.find('img', class_="lazy").get('content')
AttributeError: 'NoneType' object has no attribute 'get'

подскажите что делаю не так?
  • Вопрос задан
  • 131 просмотр
Пригласить эксперта
Ответы на вопрос 2
ThunderCat
@ThunderCat
{PHP, MySql, HTML, JS, CSS} developer
pic_href = items.find('img', class_="lazy").get('href') // почему вообще там контент должно быть???
Ответ написан
@alexbprofit
Junior SE
driver.find_elements(By.NAME, 'img').get_attribute('content')
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы