简体   繁体   中英

How to extract a specific data using python requests module

I'm trying to extract the market close for the past 5 days of a certain stock in order to input their values into a function. So far I've got:

import requests
stock_url = "https://query1.finance.yahoo.com/v7/finance/download/BYND"
stock = 'BYND'
params = {
    'range':'6d',
    'interval': '1d',
    'events': 'history'
}
data = requests.get(stock_url.format(stock), params)
print(data.text)

And what I get is:

Date,Open,High,Low,Close,Adj Close,Volume
2021-05-26,124.000000,127.879997,121.269997,126.739998,126.739998,5569000
2021-05-27,134.389999,146.800003,133.539993,142.610001,142.610001,21735600
2021-05-28,149.979996,154.399994,143.000000,145.419998,145.419998,16985000
2021-06-01,142.899994,143.360001,132.130005,135.190002,135.190002,8631500
2021-06-02,134.593994,151.360001,134.593994,149.740005,149.740005,9488200
2021-06-03,145.000000,149.639999,140.110001,143.059998,143.059998,4840900

I'd like to only get the Close column or be able to manipulate (ex: print) specific cells but have no idea how to do so.

Converting this data to Pandas Dataframe will work here:

import pandas as pd

data = """
Date,Open,High,Low,Close,Adj Close,Volume
2021-05-26,124.000000,127.879997,121.269997,126.739998,126.739998,5569000
2021-05-27,134.389999,146.800003,133.539993,142.610001,142.610001,21735600
2021-05-28,149.979996,154.399994,143.000000,145.419998,145.419998,16985000
2021-06-01,142.899994,143.360001,132.130005,135.190002,135.190002,8631500
2021-06-02,134.593994,151.360001,134.593994,149.740005,149.740005,9488200
2021-06-03,145.000000,149.639999,140.110001,143.059998,143.059998,4840900
"""
print("*"*50)
print("Data: ")
print(data)
df = pd.DataFrame([x.split(',') for x in data.split('\n')])
print("*"*50)
print("Pandas Dataframe: ")
print(df)
print("*"*50)
print("Particular Column: ")
print(df[4])

Output:

**************************************************
Data: 

Date,Open,High,Low,Close,Adj Close,Volume
2021-05-26,124.000000,127.879997,121.269997,126.739998,126.739998,5569000
2021-05-27,134.389999,146.800003,133.539993,142.610001,142.610001,21735600
2021-05-28,149.979996,154.399994,143.000000,145.419998,145.419998,16985000
2021-06-01,142.899994,143.360001,132.130005,135.190002,135.190002,8631500
2021-06-02,134.593994,151.360001,134.593994,149.740005,149.740005,9488200
2021-06-03,145.000000,149.639999,140.110001,143.059998,143.059998,4840900

**************************************************
Pandas Dataframe: 
0           1           2  ...           4           5         6
0                    None        None  ...        None        None      None
1        Date        Open        High  ...       Close   Adj Close    Volume
2  2021-05-26  124.000000  127.879997  ...  126.739998  126.739998   5569000
3  2021-05-27  134.389999  146.800003  ...  142.610001  142.610001  21735600
4  2021-05-28  149.979996  154.399994  ...  145.419998  145.419998  16985000
5  2021-06-01  142.899994  143.360001  ...  135.190002  135.190002   8631500
6  2021-06-02  134.593994  151.360001  ...  149.740005  149.740005   9488200
7  2021-06-03  145.000000  149.639999  ...  143.059998  143.059998   4840900
8                    None        None  ...        None        None      None

[9 rows x 7 columns]
**************************************************
Particular Column: 
0          None
1         Close
2    126.739998
3    142.610001
4    145.419998
5    135.190002
6    149.740005
7    143.059998
8          None
Name: 4, dtype: object
> 

Explore more on the pandas framework for various operations that you can perform on dataframe and extract your required data out of it in the way you wanted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM