Question

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
Was it helpful?

Solution

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"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top