D
D
Dmitry2020-05-24 17:35:58
Python
Dmitry, 2020-05-24 17:35:58

Why doesn't the parser output anything?

Good afternoon, I wrote a code for parsing the main page of Yandex. I want to parse the top 5 news items above the search bar. Here is my code:

Headers = {'тут user-agent'}
url = 'https://yandex.ru/'

response = requests.get(url, headers=Headers)
soup = BeautifulSoup(response.text, 'html.parser')
items = soup.find_all('a', class_='home-link list__item-content home-link_black_yes')
for link in items[:5]:
    print(link.text)

Why is the parser not outputting anything?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dimonchik, 2020-05-24
@dimonchik2013

Yandex protection
test :
print(response .content)

V
Vladimir Proskurin, 2020-05-24
@Vlad_IT

Here is the code

soup.find_all('a', class_='home-link list__item-content home-link_black_yes')

searches for elements that have all of these enumerated classes and no more. those. here is the next element to be found
<a class="home-link list__item-content home-link_black_yes" ...

And the one on your Yandex page will not be found
<a class="home-link list__item-content list__item-content_with-icon home-link_black_yes"

Read here https://www.crummy.com/software/BeautifulSoup/bs4/... it describes what needs to be done in your case.

U
Uno, 2020-05-24
@Noizefan

in 4 beautifulSoup'e in my opinion, selection by class occurs differently.
.find('a', {'class':'abcdef'})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question