N
N
nauq0k2022-04-05 22:50:46
Parsing
nauq0k, 2022-04-05 22:50:46

I want to draw a joke from the site, but it gives an error, what am I doing wrong?

import requests
from bs4 import BeautifulSoup

def get_first_news():
   headers = {
       'user-agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36 OPR/84.0.4316.52'
   }

   url = 'https://www.anekdot.ru/random/anekdot/'
   r = requests.get(url=url, headers=headers)

   soup = BeautifulSoup(r.text, 'lxml')

   anecdot = soup.find_all('div', class_="topicbox")

   for article in anecdot:
       article_title = article.find('div', class_="text").text.strip()
       print(f'{article_title}')

get_first_news()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
datka, 2022-04-06
@nauq0k

import requests
from bs4 import BeautifulSoup

def get_first_news():
   headers = {
       'user-agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36 OPR/84.0.4316.52'
   }

   url = 'https://www.anekdot.ru/random/anekdot/'
   r = requests.get(url=url, headers=headers)

   soup = BeautifulSoup(r.text, 'html.parser')

   anecdot = soup.find_all('div', class_="text")

   for article in anecdot:
       article_title = article.text.strip()
       print(article_title)

get_first_news()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question