【问题标题】:How to write scraped data into a csv file via python?如何通过python将抓取的数据写入csv文件?
【发布时间】:2020-12-23 14:43:52
【问题描述】:

我通过以下代码下载了历史股票数据。

url = "https://query1.finance.yahoo.com/v7/finance/download/RELIANCE.BO?period1=1577110559&period2=1608732959&interval=1d&events=history&includeAdjustedClose=true"
r = requests.get(url)

然后我尝试通过此代码将其写入csv 文件。

open('ril.csv').write(r.content)

但它给出了错误提示

TypeError: write() argument must be str, not bytes

【问题讨论】:

    标签: python-3.x url python-requests data-analysis stock


    【解决方案1】:

    修改代码:

    url = "https://query1.finance.yahoo.com/v7/finance/download/RELIANCE.BO?period1=1577110559&period2=1608732959&interval=1d&events=history&includeAdjustedClose=true"
    r = requests.get(url)
    open('ril.csv','wb').write(r.content)
    

    数据是以二进制形式下载的,所以我们也需要以二进制形式读取和写入。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-21
      • 2017-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-30
      • 2019-03-06
      • 2016-04-07
      相关资源
      最近更新 更多