简体   繁体   中英

AttributeError: 'NoneType' object has no attribute 'findAll' - Python

I'v been trying to figure this out for days, it's driving me crazy! I'm trying to get all the funds name in the table from Morningstars website (leftmost column), but I keep being told:

"AttributeError: 'NoneType' object has no attribute 'findAll'". 

Maybe I'm focusing on the wrong table class, but not sure. Example of the first name from Morningstar: 明治安田 DC日本債券オープン 『愛称:DCしあわせ宣言』

Please see below

    import bs4 as bs
    import pickle
    import requests

    # gather data into variable
    def save_DC_names():
       resp = requests.get('http://www.morningstar.co.jp/FundData/DetailSearchResult.do?pageNo=1')
       soup = bs.BeautifulSoup(resp.text,"lxml")
       table = soup.find('table',{'class': "table1f"})
       tickers = []
       for row in table.find_all('tr')[1:]:
          ticker = row.find_all('td')[0].text
          tickers.append(ticker)

       with open("DCtickers.pickle","wb") as f: 
          pickle.dump(tickers,f)

       print(tickers)

       return tickers

   save_DC_names()

The problem here is that soup.find is returning None. The class of None is NoneType, and NoneType doesn't have find_all or anything similar.

soup.find is returning None because there's no table in the document that matches what you've asked for. In fact, I've looked at it and there are no HTML tables at all. It looks like there's a table, because there's an HTML comment that contains HTML table markup, but comments are treated as opaque by any reasonable parser.

If you really want to parse the HTML inside the comment, you can get all the comments with

comments = soup.find_all(string=lambda text:isinstance(text,bs.Comment))

You'll then have to find the appropriate comment and then parse it with beautiful soup again. Since this HTML is in a comment, there's no guarantee that it's valid HTML.

From BS4 documentation:

AttributeError: 'NoneType' object has no attribute 'foo' - This usually happens because you called find() and then tried to access the .foo` attribute of the result. But in your case, find() didn't find anything, so it returned None, instead of returning a tag or a string. You need to figure out why your find() call isn't returning anything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM