如何通过python将抓取的数据写入csv文件?

萨马尔·普拉塔普·辛格

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

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
萨马尔·普拉塔普·辛格

修改后的代码:

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)

数据是以二进制形式下载的,所以我们也需要以二进制形式对其进行读写。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章