【问题标题】:Retrieve data from URL but receive error: iterator should return strings, not bytes从 URL 检索数据但收到错误:迭代器应返回字符串,而不是字节
【发布时间】:2018-02-15 14:29:36
【问题描述】:

我正在尝试让这个 github 上的这个 python 脚本工作。

https://gist.github.com/lebedov/f09030b865c4cb142af1

脚本从 Google 检索股票报价。

当我运行函数 stock = get_google_finance_intraday("AAPL") 时,我收到错误 _csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)

我追踪到导致错误的代码行。

这是第 39 行。

reader = csv.reader(page.content.splitlines())

我正在使用 python v3.6。我怀疑代码在 python v2 上运行,但不是在 v3 上运行。

【问题讨论】:

    标签: python python-3.x iterator


    【解决方案1】:

    但以下情况确实适用于日内价格: https://finance.google.com/finance/getprices?q=IBM&p=1d&i=60&f=d,o,h,l,c,v 注意 URL 的变化。

    【讨论】:

      【解决方案2】:

      现在看来盘中只返回每日价格行不通 尝试这个: https://www.google.com/finance/getprices?i=60&p=10d&f=d,o,h,l,c,v&df=cpct&q=IBM

      【讨论】:

        【解决方案3】:

        python 3 要求您将字节数组(来自文件的数据)解码为实际字符串

        data = data.decode('utf8')
        

        您需要在将提取的行传递给 csv 解析器之前对其执行此操作。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-01-20
          • 2013-04-21
          • 1970-01-01
          • 2017-01-29
          • 1970-01-01
          • 2023-01-22
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多