A
A
Alexander Kovalenko2021-03-18 21:51:24
Python
Alexander Kovalenko, 2021-03-18 21:51:24

When parsing, it throws an error, although everything is parsed and cannot parse links to profiles?

from bs4 import BeautifulSoup
import requests


def parse():
    URL = 'https://www.olx.pt/tecnologia-e-informatica/'
    HEADERS = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36'
    }

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

    for item in items:
        olx_ship = item.find('span', class_='promo-label promo-label--ctt inlblk rel')
        if olx_ship:
            pass
        else:
            pass
        try:
            comps.append(
                {
                #получение названия
                'title': item.find('a', class_='marginright5 link linkWithHash detailsLink').get_text(strip=True),
                #получения цены
                'price': item.find('p', class_='price').get_text(strip=True),
                # получения ссылки
                'link': item.find('a', class_='marginright5 link linkWithHash detailsLink').get('href'),
                # получения города
                'city': item.find('small', class_='breadcrumb x-normal').find_next('span').get_text(strip=True)
                })
        except:
            pass

    for user in comps:
        try:
            r = requests.get(user["link"], headers=HEADERS)
            soup_2 = BeautifulSoup(r.text, 'html.parser')
            name = soup_2.find('a', class_='user-offers').get('href')
            comps.append({'user': name})
        except:
            pass

    for comp in comps:
        print(f'{comp["title"]} -> Price: {comp["price"]} -> Link: {comp["link"]} -> City: {comp["city"]}')

            # -> Price: {comp["price"]} -> Link: {comp["link"]} -> City: {comp["city"]}


parse()

Conclusion:
Dell K17A Thunderbolt USB-C Docking Station -> Price: 45 € -> Link: https://www.olx.pt/anuncio/dell-k17a-thunderbolt-u... -> City: Negociável

Traceback (most recent call last):
File "E:\soft\parser portugal\main.py", line 53, in
parse()
File "E:\soft\parser portugal\main.py", line 48, in parse
print(f '{comp["title"]} -> Price: {comp["price"]} -> Link: {comp["link"]} -> City: {comp["city"]}')
KeyError: ' title'

Process finished with exit code 1
6053a09ce551a636022863.png

and as we see that the last product matches and then the error
and when parsing the profile links on the product page does not find them, or if it finds, then everyone is the same

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2021-03-18
@bacon

You yourself added such a dictionary there. By the way, for such an addition inside the iteration, as well as for such an except, they kick, hard. And you also learn how to debug, for example, just print comp before the place where the error is. comps.append({'user': name})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question