【问题标题】:How can I parse an empty dataframe from webscraping? NoneType error如何从网络抓取中解析空数据框?无类型错误
【发布时间】:2021-03-09 00:35:24
【问题描述】:

解析数据帧中所有抓取结果的代码是:

class GameData:

    def __init__(self):
        self.date = []
        self.time = []
        self.game = []
        self.score = []
        self.home_odds = []
        self.draw_odds = []
        self.away_odds = []
        self.country = []
        self.league = []
    .....
    
    game_data = GameData()
    
    ....


    if __name__ == '__main__':
    
        results = None
    
        for url in urls:
            game_data = parse_data(url)
            result = pd.DataFrame(game_data.__dict__)
            if results is None:
                results = result
            else:
                results = results.append(result, ignore_index=True)

有时页面没有任何数据,因此数据框返回为NoneType,我收到错误:

result = pd.DataFrame(game_data.__dict__)
AttributeError: 'NoneType' object has no attribute '__dict__'

如何仅使用已定义的标头解析空数据帧:

【问题讨论】:

    标签: python loops web-scraping selenium-chromedriver


    【解决方案1】:

    您的代码甚至在 Pandas 有机会解析数据之前就中断了:game_dataNone,因此它不能有属性 (__dict__)。因为不追加结果没有效果,所以可以跳过一个空数据。

            for url in urls:
                game_data = parse_data(url)
                if game_data is None:
                    continue
                ...
    

    【讨论】:

      猜你喜欢
      • 2019-04-02
      • 1970-01-01
      • 2017-05-17
      • 1970-01-01
      • 2016-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多