@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, '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()
  • Вопрос задан
  • 374 просмотра
Решения вопроса 1
datka
@datka
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()
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы