Home>

Code:

import requests
from bs4 import BeautifulSoup
URL= 'https://auto.ru/schelkovo/cars/hyundai/used/'
HEADERS= {
    'user-agent': 'Mozilla /5.0 (Windows NT 10.0; Win64; x64) AppleWebKit /537.36 (KHTML, like Gecko) Chrome /95.0.4638.69 Safari /537.36',
    'accept': '* /*'}
HOST= 'https://auto.ru'
def get_html (url, params= None):
    r= requests.get (url, headers= HEADERS, params= params)
    return r
def get_content (html):
    soup= BeautifulSoup (html, 'html.parser')
    items= soup.find_all ('div', class _= 'ListingItem')
    cars= []
    for item in items:
        cars.append ({
            'title': item.find ('h3', class _= 'ListingItemTitle ListingItem_title'). get_text (strip= True),
            'link': HOST + item.find ('a', class _= 'Link ListingItemTitle_link'). get ('href'),
            'rub_price': item.find ('div', class _= 'ListingItem_columnCellPrice'). get_text (),
            'city': item.find ('div', class _= 'ListingItem__additionalInfo'). find_next ('span'). get_text (),})
    return cars
def parse ():
    html= get_html (URL)
    if html.status_code== 200:
        cars= get_content (html.text)
    else:
        print ('Error')

Error:

Traceback (most recent call last):
  File "C: \ Users \ User \ PycharmProjects \ parse \ main.py", line 38, in <
module >
    parse ()
  File "C: \ Users \ User \ PycharmProjects \ parse \ main.py", line 33, in parse
    cars= get_content (html.text)
  File "C: \ Users \ User \ PycharmProjects \ parse \ main.py", line 23, in get_content
    'title': item.find ('h3', class _= 'ListingItemTitle ListingItem_title'). get_text (strip= True),
AttributeError: 'NoneType' object has no attribute 'get_text'
Process finished with exit code 1

If you want to help you, write what specifically does not work and what is the error

dtroyan2021-11-16 10:41:22

Traceback (most recent call last): File "C: \ Users \ User \ PycharmProjects \ parse \ main.py", line 38, in parse () File "C: \ Users \ User \ PycharmProjects \ parse \ main .py ", line 33, in parse cars= get_content (html.text) File" C: \ Users \ User \ PycharmProjects \ parse \ main.py ", line 23, in get_content 'title': item.find ('h3 ', class _=' ListingItemTitle ListingItem_title '). get_text (strip= True), AttributeError:' NoneType 'object has no attribute' get_text 'Process finished with exit code 1

Армен2021-11-16 10:44:58

Obviously, the ListingItemTitle ListingItem_title item was not found.

GrAnd2021-11-16 11:48:40