I am successful in getting all the Headlines from a news site, but after it fetches all the data, the python programme crashes with the following error :
Traceback (most recent call last):
File "c:\Users\HP\Desktop\My Python Projects\pratidintime.py", line 14, in <module>
print(title.text.strip())
AttributeError: 'NoneType' object has no attribute 'text'
This is my code :
import requests
from bs4 import BeautifulSoup
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36'}
r = requests.get('https://www.pratidintime.com/category/pratidin-exclusive/', headers = headers)
soup = BeautifulSoup(r.text, 'lxml')
item_inner = soup.find_all('div', {'class' : 'item-inner'})
for item in item_inner :
title = item.find('h2', {'class': 'title'})
print(title.text.strip())
This is the Output received :
#Breaking SI Exam Scam: Dibon Deka Arrested
Is Assam Winning the Coronavirus Fight?
Suspended RS MPs Stage Protest in Parliament
HPC Liquidator Plea For Liquidation Of Mills
Will Assam See More Than 3000 COVID Deaths?
Shiladitya ‘made’ into the Time magazine
#EXCLUSIVE Ranjan Gogoi dismissed Tarun Gogoi claim
Ravi out, IB in to get Naga Talk back on track
Traceback (most recent call last):
File "c:\Users\Zed\Desktop\My Python Projects\random_stuffs\pratidintime.py", line 14, in <module>
print(title.text.strip())
AttributeError: 'NoneType' object has no attribute 'text'
将
for
循环更改为这应该可以避免错误
相关问题 更多 >
编程相关推荐