AttributeError:“NoneType”对象没有属性“findAll”-Python

2024-04-30 02:27:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经想了好几天了,我都快疯了!我正试图从晨星网站(最左边的专栏)获取表中的所有基金名称,但我一直被告知:

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

也许我把注意力放在了错误的桌子上,但不确定。晨星的名字示例:DC

请看下面

    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()

Tags: textimportbsnamessaveastablefind
1条回答
网友
1楼 · 发布于 2024-04-30 02:27:03

这里的问题是汤。find没有返回。None的类是NoneType,NoneType没有find-all或类似的东西。

soup.find不返回任何值,因为文档中没有符合您要求的表。事实上,我已经看过了,根本没有HTML表。它看起来像有一个表,因为有一个包含HTML表标记的HTML注释,但是任何合理的解析器都会将注释视为不透明的。

如果您真的想解析注释中的HTML,可以使用

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

然后你必须找到合适的注释,然后再次用漂亮的汤解析它。因为这个HTML在注释中,所以不能保证它是有效的HTML。

相关问题 更多 >