-1

I would like to gather stock data from Yahoo Finance using Python 3.x.

Currently I use the following data strings:

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))

This code gives me the following return:

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

I would like to receive the following return:

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

Is there anyone who could help me with this issue?

petezurich
  • 9,280
  • 9
  • 43
  • 57
Hoogoo
  • 15
  • 1
  • 8
  • 1
    Hi welcome to stack overflow. I am not quite sure what is your question there? The wanted output and the current output look similar. Maybe try to phrase what is wrong and it should do. Is it to add a column named `Ticker` with `AAC` in it? – Sylhare Nov 18 '18 at 15:02

3 Answers3

1

You can use python methods to format the incoming text as you like. I am using split() to split the text at newlines, a terniary inside a generator comprehension to prepend either 'Ticker,' or the 'AAC,' part and join() to glue it back together (links to doku see below):

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)

Output:

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:

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
0

You could use string.replace() method. In here this would be like this: textToPrint = response.text.replace("\n", "\n".join(s))
Then print(textToPrint)

vrtex
  • 165
  • 9
0

I wouldn't use Yahoo Finance. It used to be a deep and rich resource for data, but most of the functionality was turned off almost 2 years ago. There are several viable alternatives out there. Here's one.

import pandas_datareader.data as web
from datetime import datetime
f = web.DataReader('AAC', 'robinhood')
f.head()
ASH
  • 20,759
  • 19
  • 87
  • 200