2

Ошибки в консоли нет, однако в консоли нет и конечного кода.
Все что выводиться в консоли: Process finished with exit code 0
Сам код:

import requests
from bs4 import BeautifulSoup


def parse():
    URL = 'https://www.olx.ua/elektronika/kompyutery-i-komplektuyuschie/'
    HEADERS = {
        'User_Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36 OPR/72.0.3815.465'
    }
    response = requests.get(URL, HEADERS)
    soup = BeautifulSoup(response.content, 'html.parser')
    items = soup.find_all('div', class_='offer-wrapper')
    comps = []
    for item in items:
        comps.append({
            'title': item.find('a', class_='marginright5 link linkWithHash detailsLink linkWithHashPromoted').get_text,
            'price': item.find('p', class_='price').get_text(strip=True)

        })
    for comp in comps:
        print(f"{comp['title']}")

parse()
2
  • 4
    Код 0 - значит, ошибок нет. Это значит, что код отработал корректно. То, что помимо этого больше нет никакого выхода, значит, коду нечего выводить для данного входа. 19 янв 2021 в 16:53
  • должна быть ошибка AttributeError: 'NoneType' object has no attribute 'get_text'
    – Jack_oS
    20 янв 2021 в 8:34

1 ответ 1

4

Чуть поправил ваш код, ошибка была в том что команды get_text() нету, только есть .text и бс4 не выдел класс ссылки (это бывает, когда класс слишком длинный). Вот сам код который работает:

import requests
from bs4 import BeautifulSoup


def parse():
    URL = 'https://www.olx.ua/elektronika/kompyutery-i-komplektuyuschie/'
    # HEADERS = {
    #     'User_Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36 OPR/72.0.3815.465'
    # }
    response = requests.get(URL)  # headers=HEADERS
    soup = BeautifulSoup(response.content, 'html.parser')
    items = soup.find_all('div', class_='offer-wrapper')
    comps = []
    for item in items:
        comps.append({
            'title': item.find('h3', class_='lheight22 margintop5').text.strip(),
            'price': item.find('p', class_='price').text.strip()

        })
    for comp in comps:
        print(f"{comp['title']} : {comp['price']}")

parse()

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.