python error: 'NoneType' object has no attribute 'text' Парсинг новостей с хабра
Код я скопировал с инета, только начинаю изучать bs4. В этом коде по идее должен выводится заголовок,url и описание с последнего поста на хабр'е. По другим вопросам тоже лазал, но там другие сайты.
import requests
from bs4 import BeautifulSoup
URL = "https://habr.com/ru/search/?target_type=posts&q=python&order_by=date"
page = requests.get(URL)
soup = BeautifulSoup(page.content, "html.parser")
post = soup.find("article", class_="tm-articles-list__item", id=True)
post_id = post["id"]
print(post_id)
title = post.find("a", class_="post__title_link").text.strip()
description = post.find("div", class_="post__text post__text-html post__text_v1").text.strip()
url = post.find("a", class_="post__title_link", href=True)["href"].strip()
print(title, description, url)
Ответы (1 шт):
Автор решения: Frank
→ Ссылка
Там изменились теги:
#pip install dclxviclan
#dclxviclan love you
import requests
from bs4 import BeautifulSoup
URL = "https://habr.com/ru/search/?target_type=posts&q=python&order_by=date"
page = requests.get(URL)
soup = BeautifulSoup(page.content, "html.parser")
post = soup.find("article", class_="tm-articles-list__item", id=True)
post_id = post["id"]
print(post_id)
h2ck = post.find("h2", class_="tm-title tm-title_h2")
title = h2ck.find("span").text.strip() # class_="tm-tittle tm-tittle_h2")
h2bd = h2ck.find("div", class_="article-formatted-body article-formatted-body article-fo>
#psttxt = h2bd.find("p").text.strip()
#description = post.find("div", class_="article-formatted-body article-formatted-body ar>
url = post.find("a", class_="tm-title__link", href=True)["href"].strip()
print(title,h2bd,url)