質問

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