Skip to content Skip to sidebar Skip to footer

Using The Browser's Xhr Log To Recreate An Ajax Request

I want to create a post request fully based on the XHR data that appear in the Chrome inspector(network tab). The goal is to recreate an AJAX request to go to a dynamically shown p

Solution 1:

1) filtrosJson is randomly generated code. Which is found in the source code of first page.

We first need to grab that using any method that you like, for now I am using bs4

2) The payload is in json so we ll have to use json.dumps to send the post request. And finally send the post requests indicating the Content-Type as application/json

We can do something like this,

import requests, json
from bs4 import BeautifulSoup as bs

s=requests.Session()
headers={"User-Agent":"Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36"}
s.headers.update(headers)

r=s.get("http://www.metrocuadrado.com/web/buscarFiltros/bogota-apartamento-venta")

soup=bs(r.content)
filtrosJson=soup.find(id="filtrosJson").text
data={"cantidadResultadosPagina": "16","filtroOrdenamiento": "-1","filtrosJson":filtrosJson,"token": ""}


r=s.post("http://www.metrocuadrado.com/web/busqueda/pagina-4",data=json.dumps(data),headers={"X-Requested-With":"XMLHttpRequest","Content-Type":"application/json"})

print r.json()

This works for me on Python2.7, Ubuntu 14.04

Let me know if you face any issues :-)

Post a Comment for "Using The Browser's Xhr Log To Recreate An Ajax Request"