M
M
Maxim Zhurba2020-11-13 01:50:26
Python
Maxim Zhurba, 2020-11-13 01:50:26

An error in the parser code, what is the error?

I'm just starting to get acquainted with python, I did everything according to the example, tell me how to correctly determine the 'title'

from bs4 import BeautifulSoup
import requests

def parse():
    URL = ''
    HEADERS = {
        ''
    }

    response = requests.get(URL, headers = HEADERS)
    soup = BeautifulSoup(response.content, 'html.parser')
    items = soup.findAll('div', class_ = 'offer-wrapper')
    comps = []

    for item in items:
        comps.append({
            'tittle' : item.find('a', class_ = 'marginright5 link linkWithHash detailsLink linkWithHashPromoted').get_text(strip = True)
        })

    for comp in comps:
        print(comp['title'])

parse()


5fadbba16966b094372545.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2020-11-13
@Kamikaze

item.find('a', class_ = 'marginright5 link linkWithHash detailsLink linkWithHashPromoted').get_text(strip = True)

Recently I saw a wonderful name for a similar design, "train wreck." The first car fell down - the whole train derailed.
The bottom line is, the find method, if it does not find what is described in the conditions, will return None, from which the get_text method will fall, which happened to you. In an amicable way, it would be necessary to check whether the find method returned something and only if it returned, get its text.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question