I am trying to exctract titles & headers from web pages,But i get only one element.

from BeautifulSoup import BeautifulSoup

import urllib2 
url = urllib2.urlopen("http://timesofindia.indiatimes.com/")

content = url.read()
patFinderTitle = re.compile('')
listIterator = []

listIterator[:] = range(2,16)

soup2 = BeautifulSoup(content)

soup2.findAll("title")
print soup2.findAll("h1")




for i in soup2.findAll("title"):
  print titleSoup[i]

  print "\n"

I am getting the error in for loop ie.

TypeError: list indices must be integers, not Declaration
有帮助吗?

解决方案

If you want to print all title tag, simply print i. (i is not an index. Each title element is assigned to i while iterating the findAll result):

for i in soup2.findAll("title"):
    print i
    print "\n"
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top