A
A
Alexander Kovalenko2021-03-17 20:35:33
Python
Alexander Kovalenko, 2021-03-17 20:35:33

Why is href not parsed in the tag?

The bottom line is that there is a href in the a tag - a link, if you just display the text, then it can be seen that there is a link, when I add .get('href'), writes None, what could be the problem? Here is the code

import requests
from bs4 import BeautifulSoup


def parse():
    URL = 'https://www.olx.ro/moda-frumusete/incaltaminte-barbati/?view=list'
    HEADERS = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36'
    }
    response = requests.get(URL, headers=HEADERS)
    soup = BeautifulSoup(response.content, 'html.parser')
    items = soup.findAll('div', class_='offer-wrapper')
    ads = []
    for item in items[:5]:
        ads.append({
            # 'title': item.find('h3', class_='lheight22 margintop5').get_text(strip=True),
            'link': item.find('a', class_='marginright5 link linkWithHash detailsLink'),
            # 'price': item.find('p', class_='price').get_text(strip=True)
        })
    print(ads)


parse()


Output
[{'link': None}, {'link': None}, {'link': None}, {'link': None}, {'link': None}]
even though there are links
60523de0d2bc8354841821.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-03-17
@SoreMix

There is no such
class

for item in items[:5]:
    print(item.find('a').attrs)

and admire

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question