在python中,我有一個程序從URL列表中返回。想要返回<title>標記,但返回<title>錯誤請求<.title> Python 3
有些人返回時,他們被一起放在一個列表
例如我裝載兩個URL到到文本文件錯誤請求:
http://www.scientific.net/MSF
http://www.scientific.net/JMNM
它返回:
<title>Bad Request</title>
<title>Journal of Metastable and Nanocrystalline Materials</title>
如果我只有列表中的第一個URL,代碼就可以正常工作。我如何讓它檢索標題而不是錯誤的請求?
我的代碼:
url_list= []
f = open('test.txt','r') #text file with url
for line in f:
url_list.append(line)
for link in url_list:
try:
r = requests.get(link)
soup = BeautifulSoup(r.content,"html.parser")
title = soup.title
title.string = title.get_text(strip = True)
print(str(title))
except:
print("No Title Found ")
continue