python提示 AttributeError: 'NoneType' object has no attribute 'text'

在抓取新闻标题时,用article.h2可以显示出新闻标题,但加上article.h2.text, 就出现如标题错误

import pandas as pd
import requests
import re
from bs4 import BeautifulSoup
from newspaper import Article

URL = "https://www.thepaper.cn/channel_25950"
r = requests.get(URL)
soup = BeautifulSoup(r.content, 'html5lib')
contents = []  # a list to store contents

table = soup.find('div', {'id': 'mainContent'})
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/803789388136155.png "=600 #left")

# print(table) #for verifying result
news = table.findAll('div',attrs={'class': 'news_li'})
# print(news)
for article in news:
 try:
    content = {}
    content['Title'] = article.h2
    contents.append(content)
 except IndexError:
    pass

print(contents)



img

img

因为最后一个news_li没有h2对象,所以article.h2为None,在调用text就出错了。需要先判断h2是否存在在获取text内容

img


有帮助麻烦点个采纳【本回答右上角】,谢谢~~

for article in news:
 try:
    content = {}
    if article.h2!=None:#判断不是None在获取text
        content['Title'] = article.h2.text
        contents.append(content)
 except IndexError:
    pass
print(contents)