문제

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