简体   繁体   中英

Using Python Requests library to request Cookie as JSON in order to format as DataFrame

I'm trying to use Python Requests to retrieve a cookie from a website using a POST requests and JSON request parameters.

I'm using KNIME, which allows you to output a response as a DataFrame. When I request the cookie & attempt to output it as a DataFrame with the following code:

from pandas import DataFrame
import requests

session = requests.Session()

url = "https://api.danmurphys.com.au/apis/ui/Address/SetFavouriteStore"
payload = {"StoreNo":"1276"}

x = session.post(url, json=payload)

output_table = DataFrame(x.cookies)

, I get the following error:

Execute failed: No serializer extension having the id or processing python type "http.cookiejar.Cookie" could be found.
Unsupported column type in column: "0", column type: "<class 'http.cookiejar.Cookie'>".

I know the DataFrame output function would work if the response was JSON, so I've tried the following code:

x = session.post(url, json=payload)
res = session.cookies.get_dict()

output_table = DataFrame(res)

But this gives the following error:

ValueError: If using all scalar values, you must pass an index

If anyone knows how to format these cookies for DataFrame, or if I should be using a different library to Requests please inform me. Thanks.

EDIT: using the following DataFrame constructory:

output_table = DataFrame(data=session.cookies.get_dict(), index=session.cookies.get_dict(), columns=None, dtype=None, copy=False)

Is successful in outputting the cookies formatted as DataFrame however the values are replicated across rows and columns, as depicted in the screenshot. 在此处输入图片说明

Since the ValueError points at passing all scalar values with an index, you may just do this:

from pandas import DataFrame
import requests

session = requests.Session()

url = "https://api.danmurphys.com.au/apis/ui/Address/SetFavouriteStore"
payload = {"StoreNo":"1276"}

x = session.post(url, json=payload)
res = session.cookies.get_dict()
output_table = DataFrame({'data':res})#pass res as a dictionary value

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