Domanda

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
È stato utile?

Soluzione

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"
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top