【发布时间】:2021-03-09 22:58:49
【问题描述】:
我不断收到此错误,并多次检查了 html 源代码,如下所示。我很确定我找到了正确的元素,所以文本部分在技术上应该存在。
背景:构建网络爬虫;试图提取标题。奇怪的是,用于提取摘要文本和链接的代码都有效 - 只是标题无效。
from bs4 import BeautifulSoup
import requests
url = "https://www.ribbonfarm.com/"
source = requests.get(url, headers = {"User-Agent" : "Mozilla/5.0"}).text
soup = BeautifulSoup(source, 'lxml')
article = soup.find("div")
headline = soup.find("h1", class_ = "entry-title").text
print (headline)
我收到的错误消息:回溯(最近一次通话最后一次): 文件“main.py”,第 9 行,在 标题 = soup.find("h1", class_ = "entry-title").text AttributeError: 'NoneType' 对象没有属性 'text'
谢谢!
【问题讨论】:
-
article = soup.find("div")..哪个div? -
@AbhishekRai 嗯... Corey Schafer 使用了一个更独特的元素,称为“文章”(github.com/CoreyMSchafer/code_snippets/blob/master/…),但我找不到,所以我只使用了 div。该特定文章的 html 代码是 ```` div class="post-7348 post type-post status-publish format-standard has-post-thumbnail hentry category-general series-captains-log entry" ```` 其中感觉不对,因为它是针对那篇文章的,而不是对页面上的每一篇文章都通用。有什么建议吗?
-
奇怪的是,我对摘要文本和链接的抓取仍然有效。所以我不认为这是主要问题,但也不知道是什么。
-
您到底想从页面中获得什么?
MJD 59,143..这些? -
soup.find_all("h1", class_ = "entry-title")得到什么结果(注意我这里没有使用.text,我想查看实际的标签)?
标签: python html web-scraping beautifulsoup nonetype