Search code examples
pythongettext

AttributeError: 'NoneType' object has no attribute 'get_text', I've tried some other answers from the platform,but it is useless


Here is my code:

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)

When I executed it, it got some news from the website. However,here is an error at the end of these news. You can see:

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'

what's wrong with my code? Wait a kind guy! Thank you! Another problem is "how to delete the js code", When I executed it, there are some in my news,like this: I can't send two links,so I delete"//".

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


Solution

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

    If there's no div tag with mod-content class below CotnentBSobj, ContentBSobj.find(..) returns None

    Trying to access from get_text attribute from None object cause the AttributeError:

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

    To prevent the exception, you should check none before call get_text():

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