cmbramwell
cmbramwell

Reputation: 77

Loading a requests text response into a Pandas dataframe

Trying to load text from a requests response into a Pandas dataframe.

url = "https://api.semrush.com/"

parameters = {"type": "phrase_organic",
                "key": "*****",
                "phrase": phrase,
                "database": "us",
                "display_limit": 2,
                "export_columns": "Dn,Ur"}

response = requests.get(url, params=parameters)
urldata = response.text

dF = pd.read_csv(urldata)

The response text looks like this...

Domain;Url
facebook.com;https://facebook.com/home
instagram.com;https://instagram.com/home

The text is separated by semicolons. Domain and Url (first line) should be the name of the pandas columns. Everything else will be rows in the dataframe.

Upvotes: 4

Views: 3607

Answers (1)

RobinFrcd
RobinFrcd

Reputation: 5436

pd.read_csv takes a file or a buffer as input, not a plain string directly.

You can either save your data on disk first and then load it with pandas, or use StringIO:

import pandas as pd
from io import StringIO

pd.read_csv(StringIO(urldata), sep=';')

Upvotes: 4

Related Questions