Python 2.7 如何使用bs4在Python2.7中提取此网页的标题?
目前我使用的代码如下:Python 2.7 如何使用bs4在Python2.7中提取此网页的标题?,python-2.7,beautifulsoup,index-error,Python 2.7,Beautifulsoup,Index Error,目前我使用的代码如下: import urllib from bs4 import BeautifulSoup import codecs file_obj = open("E:\\Sport_Cricket.txt", "r+") links_str = file_obj.readlines() c=1 for j in links_str: url=j.rstrip('\n') if(url.endswith("ece")): htmltex
import urllib
from bs4 import BeautifulSoup
import codecs
file_obj = open("E:\\Sport_Cricket.txt", "r+")
links_str = file_obj.readlines()
c=1
for j in links_str:
url=j.rstrip('\n')
if(url.endswith("ece")):
htmltext = urllib.urlopen(url).read()
soup = BeautifulSoup(htmltext,"lxml")
#title
webpage_title = soup.find_all("h1", attrs = {"class": "title"})
webpage_title = webpage_title[0].get_text(strip=True)
with codecs.open("E:\\Corpus\\Sport\\Cricket\\text"+str(c)+".txt", "w+", encoding="utf-8") as f:
f.writelines(webpage_title+"\r\n")
c=c+1
Sport_Cricket.txt包含:
http://www.thehindu.com/sport/cricket/unadkat-does-the-trick-for-pune/article18401543.ece
http://www.thehindu.com/sport/cricket/live-updates-delhi-daredevils-versus-mumbai-indians/article18400821.ece
http://www.thehindu.com/sport/cricket/old-guard-wants-pull-out-coa-warns-of-consequences/article18400811.ece
http://www.thehindu.com/sport/cricket/the-rise-of-sandeep-sharma/article18400700.ece
http://www.thehindu.com/sport/cricket/axar-has-found-his-mojo/article18400258.ece
我得到以下错误:
Traceback (most recent call last):
File "C:\Users\PJM\working_extractor_sorted_complete.py", line 31, in <module>
webpage_title = webpage_title[0].get_text(strip=True)
IndexError: list index out of range
是否有其他选项代替网页标题=网页标题[0]。textstrip=True???Pankaj
使用此选项可以使用BeautifulSoup获取标题
webpage_title = soup.title.string
这将获得html文档中任何位置的第一个title元素