1

I am getting this error when i trying to parse through a for loop

pages=soup.find('span',{'class':'pagnDisabled'})
if pages==None:
    print 'None'
elif pages!=None:
    for i in range(2, pages + 1):
       print i

I am getting this error

TypeError: unsupported operand type(s) for +: 'Tag' and 'int'
10
  • 2
    pages isn't an int. Were you expecting an int? If so, why? Commented Jun 18, 2014 at 8:57
  • You want pages.length most likely. Commented Jun 18, 2014 at 8:57
  • @user2357112 the number iam getting as the value of pages is 37 Commented Jun 18, 2014 at 8:58
  • 2
    why not doing a "for i in pages"? Commented Jun 18, 2014 at 9:02
  • 1
    @FazeelaAbuZohra How you can get 37, because type of pages is a Tag? Commented Jun 18, 2014 at 9:04

2 Answers 2

1

.find() returns a Tag object which implements a __len__ but when you try to add a Tag object with an int it will not try to use the length of the tag so you will have to explicitly call it with len(pages), which will give you back the length of a Tag's contents.

Also, .find() only returns 1 Tag. You want the .find_all() method instead.

Sign up to request clarification or add additional context in comments.

Comments

1

soup.find() return a Tag. Maybe you must used soup.findall() method instead of soup.find() like this:

pages=soup.findall('span',{'class':'pagnDisabled'})
if len(pages) == 0:
    print 'None'
elif len(pages):
    for i in range(2, len(pages) + 1):
       print i

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.