PyNaobie
PyNaobie

Reputation: 215

Getting Value from Response from Requests on python

I am using requests and I'm trying to get the response (as seen on Network tab on chrome)

url = r"https://wwww.mylocal.com/345523"
authKey = base64.b64encode("testu:pwd$u")
headers = {"Content-Type":"application/json", "Authorization":"Basic " + "YWRtaW46OWJKTmp5WUxKc1A0dlJ1dTd2Mk0"}
data = { "id":444}
r = requests.get(url, headers=headers, params = data)
print r.content

But I keep getting the HTML of the page and I want to get actual response. When using r.content I get a message that object cannot be decoded, which makes sense since r.content retrieves the source code instead of actual response. Is there a way to get that? The response I see on network tab is similar to:

{"search_results": {"@xmlns": "http://mylocal.com/345523", "ColorsInfo": {"bgColor": "552448"}}

And what I want to retrieve is the value of bgcolor but on r.content I get the Html content of the page.

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
...and so on

Upvotes: 0

Views: 6784

Answers (2)

PyNaobie
PyNaobie

Reputation: 215

solved it! Parameter has to be a string so correct code:

url = r"https://wwww.mylocal.com/345523"
authKey = base64.b64encode("testu:pwd$u")
headers = {"Content-Type":"application/json", "Authorization":"Basic " + "YWRtaW46OWJKTmp5WUxKc1A0dlJ1dTd2Mk0"}
data = { "id":"444"}
r = requests.get(url, headers=headers, params = data)
print r.content

Upvotes: 1

berto
berto

Reputation: 8483

My first guess is that the think you are getting is a login form because you have not properly logged into the site.

Can you verify this is the case by reading through the html or writing the html into a file and opening it up in the browser.

If my suspicions are correct look at requests session object (http://docs.python-requests.org/en/latest/user/advanced/), post the username and password via form as you would in the browser and then request the data above.

Upvotes: 0

Related Questions