属性错误:'NoneType'对象没有属性'get_text',我尝试了平台的其他一些答案,但没用



这是我的代码:

enter code here
import urllib.request
import re
from bs4 import BeautifulSoup
URLdict=dict()
class M1905:
def __init__(self,baseurl):
self.baseURL=baseurl
self.user_agent = 'Chrome/58.0(compatible;MSIE 5.5; Windows 10)'
self.headers = {'User-Agent': self.user_agent}
def getPage(self,pageNum):
url=self.baseURL+'?refresh=1321407488&page='+str(pageNum)
request=urllib.request.Request(url,headers=self.headers)
response=urllib.request.urlopen(request)
first=response.read().decode('utf-8')
BSobj = BeautifulSoup(first, "html.parser")
for a in BSobj.findAll("a", href=True):
if re.findall('/news/', a['href']):
URLdict[a['href']] = a.get_text()
for link, title in URLdict.items():
print(title, ":", link)
ContentRequest = urllib.request.Request(link, headers=self.headers)
ContentResponse = urllib.request.urlopen(ContentRequest)
ContentHTMLText = ContentResponse.read().decode('utf-8')
ContentBSobj = BeautifulSoup(ContentHTMLText, "html.parser")
Content = ContentBSobj.find("div", {"class": "mod-content"})
print(Content.get_text())
return first
baseURL='http://www.1905.com/list-p-catid-221.html'
m1905=M1905(baseURL)
m1905.getPage(1)

当我执行它时,它从网站上得到了一些消息。然而,在这些新闻的末尾有一个错误。你可以看到:

enter code here
Traceback (most recent call last):
File "C:/Users/Heidy/PycharmProjects/untitled1/m1905.py", line 33, in 
<module>
m1905.getPage(1)
File "C:/Users/Heidy/PycharmProjects/untitled1/m1905.py", line 29, in 
getPage
print(Content.get_text())
AttributeError: 'NoneType' object has no attribute 'get_text'

我的代码出了什么问题?等一个善良的家伙!非常感谢。另一个问题是"如何删除js代码",当我执行它时,我的新闻中有一些,比如:我不能发送两个链接,所以我删除了"//"。

var ATLASCONFIG={id:"1191699",prevurl:"httpwww.1905.com/news/20170610/1191700.shtml#p1",nextturl:"httpwww.1905.com/news/20170610/1191700.shtml#p1",shareIframe:"http://www.1905.com/api/share2.php?id=1191699&title=%E3%8A%E6%B7%B1%E5%A4%9C%E9%A3%9F%E5%A0%82%E3%8B%E6%9B%9D%E3%80%8A%E9%B1%BC%E6%9D%BE%E9%A5%AD%E3%8%8B%E5%89%A7+%E5%BE%90%E5%A8%87%E5%88%E6%98%8A%E7%84%B6%E7%BB%93%E7%BC%98&url=http%3A%2F%2Fwww.1905.com%2F%2Newgallery%2Fhdpic%2F11191699.shtml&img=http%3A%2F%2Fimage11.m1905.cn%2上传文件%2F2017%2F2610%2F20170610020519474659_watermark.jpg;app_id=www&sign=af43563e8eacab2280a4a84f8bb016cb"}

Content = ContentBSobj.find("div", {"class": "mod-content"})

如果CotnentBSobj下面没有mod-content类的div标签,则ContentBSobj.find(..)返回None

尝试从None对象的get_text属性访问导致AttributeError:

>>> Content.get_text
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'get_text'

为了防止异常,您应该在调用get_text():之前检查none

Content = ContentBSobj.find("div", {"class": "mod-content"})
if Content is not None:
print(Content.get_text())

最新更新