我想使用Python 3.x从Yahoo Finance收集股票数据。
当前,我使用以下数据字符串:
remove_rows(xdf, cat, ~B:C+F+G:I)
## # A tibble: 20 x 4
## cat val1 val2 val3
## <chr> <int> <int> <int>
## 1 A 87 98 5
## 2 D 65 46 87
## 3 E 4 69 6
## 4 J 10 94 11
## 5 K 37 86 52
## 6 L 89 64 44
## 7 M 61 10 28
## 8 N 79 52 89
## 9 O 71 33 77
## 10 P 45 33 77
## 11 Q 56 97 29
## 12 R 10 28 39
## 13 S 25 7 71
## 14 T 86 57 51
## 15 U 92 2 15
## 16 V 25 36 12
## 17 W 90 78 10
## 18 X 20 82 90
## 19 Y 39 84 13
## 20 Z 43 93 18
这段代码给了我以下回报:
import requests url = "https://query1.finance.yahoo.com/v7/finance/download/AAC?period1=1514761200&period2=1517439600&interval=1d&events=history&crumb=Nr.jC4aJCc7" s = "AAC," response = requests.post(url) print ((s),(response.text))
我希望收到以下退货:
AAC, Date,Open,High,Low,Close,Adj Close,Volume 2018-01-16,9.370000,9.410000,9.000000,9.060000,9.060000,69400 2018-01-17,9.120000,9.170000,9.000000,9.030000,9.030000,104500 2018-01-18,9.050000,9.135000,8.980000,8.990000,8.990000,166600 2018-01-19,9.000000,9.270000,9.000000,9.200000,9.200000,110600 2018-01-22,9.200000,9.200000,8.940000,9.080000,9.080000,139200 2018-01-23,9.050000,9.110000,9.000000,9.020000,9.020000,54700 2018-01-24,9.070000,9.070000,8.910000,8.950000,8.950000,117500 2018-01-25,9.000000,9.060000,8.900000,9.050000,9.050000,204300 2018-01-26,9.110000,9.240000,9.090000,9.170000,9.170000,62000 2018-01-29,9.170000,9.780000,9.170000,9.500000,9.500000,173300 2018-01-30,9.400000,9.480000,9.000000,9.080000,9.080000,82800 2018-01-31,9.080000,9.250000,8.820000,8.950000,8.950000,122000
有没有人可以帮助我解决这个问题?
答案 0 :(得分:1)
您可以使用python方法根据需要设置输入文本的格式。我正在使用split()在换行符处分割文本,在生成器解析器内部添加了一个三进制,以'Ticker,'
或'AAC,'
部分开头,并join()
将其粘在一起(链接到doku见下文):
import requests
url = "https://query1.finance.yahoo.com/v7/finance/download/AAC?period1=1514761200&period2=1517439600&interval=1d&events=history&crumb=Nr.jC4aJCc7"
s = "AAC,"
response = requests.post(url)
text_lines = '\n'.join(('Ticker,' if x.startswith("Date") else s) + x.strip()
for x in response.text.split("\n") if x)
print(text_lines)
输出:
Ticker,Date,Open,High,Low,Close,Adj Close,Volume
AAC,2018-01-16,9.370000,9.410000,9.000000,9.060000,9.060000,69400
AAC,2018-01-17,9.120000,9.170000,9.000000,9.030000,9.030000,104500
AAC,2018-01-18,9.050000,9.135000,8.980000,8.990000,8.990000,166600
AAC,2018-01-19,9.000000,9.270000,9.000000,9.200000,9.200000,110600
AAC,2018-01-22,9.200000,9.200000,8.940000,9.080000,9.080000,139200
AAC,2018-01-23,9.050000,9.110000,9.000000,9.020000,9.020000,54700
AAC,2018-01-24,9.070000,9.070000,8.910000,8.950000,8.950000,117500
AAC,2018-01-25,9.000000,9.060000,8.900000,9.050000,9.050000,204300
AAC,2018-01-26,9.110000,9.240000,9.090000,9.170000,9.170000,62000
AAC,2018-01-29,9.170000,9.780000,9.170000,9.500000,9.500000,173300
AAC,2018-01-30,9.400000,9.480000,9.000000,9.080000,9.080000,82800
AAC,2018-01-31,9.080000,9.250000,8.820000,8.950000,8.950000,122000
Doku:
答案 1 :(得分:0)
您可以使用string.replace()方法。在这里就像这样:
textToPrint = response.text.replace("\n", "\n".join(s))
然后打印(textToPrint)
答案 2 :(得分:0)
我不会使用Yahoo Finance。它曾经是丰富而丰富的数据资源,但是几乎在2年前就关闭了大多数功能。有几种可行的替代方案。这是一个。
import pandas_datareader.data as web
from datetime import datetime
f = web.DataReader('AAC', 'robinhood')
f.head()