0
from urllib.request import urlopen
from bs4 import BeautifulSoup

html = urlopen("http://www.pythonscraping.com/pages/page3.html")
bsObj = BeautifulSoup(html,"html.parser")

for child in bsObj.find("table",{"id":"giftlist"}).children:
    print(child)

Could anyone tell me what wrong with my code is? :((( What should i do next?

ILostMySpoon
  • 2,399
  • 2
  • 19
  • 25
vancouverde
  • 37
  • 2
  • 9
  • 1
    `find` returned None. – ILostMySpoon Jun 07 '17 at 21:29
  • 1
    `bsObj.find("table",{"id":"giftlist"})` is evaluating to `None`. – cs95 Jun 07 '17 at 21:31
  • 1
    To be clearer, bsObj.find("table",{"id":"giftlist"}) returned a None object. You then tried to call the children method of the None object which caused the error. You need to check the results of the find method before committing it to this loop. Or put the loop inside a try and handle the exception. – Alan Leuthard Jun 07 '17 at 21:33
  • so what should i do now? could you maybe help me? – vancouverde Jun 07 '17 at 21:34
  • i just put the loop insinde a try like this : `try: for child in bsObj.find("table",{"id":"giftlist"}).children: print(child) except Exception as e: print("Tag was not found")` it just printed "Tag was not found" . How could i fix the problem here? – vancouverde Jun 07 '17 at 21:38
  • 1
    Possible duplicate of [Python: Attribute Error - 'NoneType' object has no attribute 'something'](https://stackoverflow.com/questions/8949252/python-attribute-error-nonetype-object-has-no-attribute-something) – Prune Jun 07 '17 at 21:59
  • What problem is that? Your try worked exactly as you wrote it. Your ultimate problem might be that bsObj.find() returns None. There is zero information here for anyone to help you with that problem. All I can say is you need to find some html that has at least 1 table with id "giftlist". Until then, your find will always return None. – Alan Leuthard Jun 07 '17 at 22:56
  • there is already a table with id "giftlist", you can open it on googleChrome and see !!! – vancouverde Jun 07 '17 at 23:03

4 Answers4

1

You should put the code in try-except block

from urllib.request import urlopen
from bs4 import BeautifulSoup

html = urlopen("http://www.pythonscraping.com/pages/page3.html")
bsObj = BeautifulSoup(html,"html.parser")

try:
    for child in bsObj.find("table",{"id":"giftlist"}).children:
        print(child)
except AttributeError as e:
    print(e)
except:
    print("An error has occured")

***In your case I have visited the website the id is not "giftlist", it's "giftLift" you have done a typing mistake and that's why find function is returning none type object.

Rajat_boss
  • 11
  • 1
1

I'm not sure whether you have already solved this problem posted 3 years ago, but you've made a small mistake, I'm assuming.

The id of the tag is not giftlist... It is giftList

Is your code from the book "Web Scraping With Python" from the O'Reilly series? I found the exact same code from that book, including this webpage, pythonscraping.com/pages/page3.html, which is posted by the author for the purpose of giving the readers a place to practice. Btw on the book it is also giftList, so I think you might have copied the code wrong

try this one now

for child in bsObj.find("table",{"id":"giftList"}).children:
print(child)
cliff_leaf
  • 118
  • 10
0

One option is to put the offending loop construct into a try, then handle the exception that appears when the interator returns None:

from urllib.request import urlopen
from bs4 import BeautifulSoup

html = urlopen("http://www.pythonscraping.com/pages/page3.html")
bsObj = BeautifulSoup(html,"html.parser")

try:
    for child in bsObj.find("table",{"id":"giftlist"}).children:
        print(child)
except AttributeError:
    # do what you want to do when bsObj.find() returns None

Or you could check the resulting list for None before entering the loop:

from urllib.request import urlopen
from bs4 import BeautifulSoup

html = urlopen("http://www.pythonscraping.com/pages/page3.html")
bsObj = BeautifulSoup(html,"html.parser")

result = bsObj.find("table",{"id":"giftlist"})
if result:
    for child in result.children:
        print(child)
else:
    # do what you want to do when bsObj.find() returns None
Alan Leuthard
  • 798
  • 5
  • 11
0

It's typo issue. I also meet the same problem. In the web page, the id name should be "id="giftList", not giftlist. It should work after modifying the id name. Try it.