0

I tried to download specific data as part of my work, the data is located in link! .

The source indicates how to download through the get method, but when I make my requests:

    import requests
    import pandas as pd
    url="https://estadisticas.bcrp.gob.pe/estadisticas/series/api/PN01210PM/csv/2015-01/2019-01"
    r=pd.to_csv(url)

it doesnt read as it should be (open link in navigator).

When I try

    s=requests.get(url,verify=False) # you can set verify=True
    df=pd.DataFrame(s)

the data neither is good.

What else can I do? It suppose to download the data as csv avoiding me to clean the data.

L F
  • 548
  • 1
  • 7
  • 22
  • You are probably looking for `pandas.read_csv()`. Check https://stackoverflow.com/questions/32400867/pandas-read-csv-from-url – Dustin Sun Jun 11 '19 at 14:22

1 Answers1

1

to get the content as csv you can replace all HTML line breaks with newline chars.

please let me know if this works for you:

import requests
import pandas as pd
from io import StringIO
url = "https://estadisticas.bcrp.gob.pe/estadisticas/series/api/PN01210PM/csv/2015-01/2019-01"
content = requests.get(url,verify=False).text.replace("<br>","\n").strip()
csv =  StringIO(content)
r = pd.read_csv(csv)

print(r)
Adam.Er8
  • 12,675
  • 3
  • 26
  • 38
  • 1
    it says `DataFrame constructor not properly called!`. if you replace `/csv/` for `/xls/` in url, it makes a download, Should not it be the same for csv? – L F Jun 11 '19 at 14:28
  • please see my edit, I'm not using `DataFrame` constructor at all now – Adam.Er8 Jun 11 '19 at 14:29