使用浏览器的XHR日志重新创建AJAX请求 [英] Using the browser's XHR log to recreate an AJAX request

查看:51
本文介绍了使用浏览器的XHR日志重新创建AJAX请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想完全根据Chrome检查器(网络"标签)中显示的XHR数据创建发布请求.目标是重新创建AJAX请求以转到动态显示的第4页.

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 page 4.

我正在这样编程:

from requests import Session
session = requests.Session()

session.head('http://www.metrocuadrado.com/web/buscarFiltros/bogota-apartamento-venta')

payload = {...}   #copied and pasted literally from the (previously inspected) source code of the XHR request
headersxhr = {...}   #dictionary of all the headers found in the (previously inspected) source code of the XHR

response = session.post(
    url = 'http://www.metrocuadrado.com/web/busqueda/pagina-4',
    data = payload,    
    headers = headersxhr
    )

print response.text

不幸的是,这给了我404错误.粘贴的有效载荷非常长,带有很多嵌套字典.它是这样开始的:

Unfortunately this gives me a 404 error. The pasted payload is very long, with a lot of nested dictionaries. It starts like this :

{令牌":","cantidadResultadosPagina":"16","filtrosJson":"\ t \ t \ n \ t \ t {\" mnombreinmobiliaria \:{\" nombre \":\"mnombreinmobiliaria \",\"valor \":[\"\"],\"valor2 \":null,\"descripcion \":\"NombreCompañia\",\"tip #.....依此类推

{"token":"","cantidadResultadosPagina":"16","filtrosJson":"\t\t\n\t\t{\"mnombreinmobiliaria\": {\"nombre\":\"mnombreinmobiliaria\",\"valor\":[\"\"],\"valor2\":null,\"descripcion\":\"Nombre Compañia\",\"tip #.....and so on

我应该注意一个编码问题吗?
另外,我是否必须遍历所有标头?

Could there be an encoding problem I should be aware of ?
Also, do I have to pass through all the headers ?

非常感谢!

推荐答案

1) filtrosJson 是随机生成的代码.可以在第一页的源代码中找到.

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

我们首先需要使用您喜欢的任何方法来获取它,现在我正在使用 bs4

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

2)有效负载在json中,因此我们将不得不使用 json.dumps 来发送发布请求.最后发送指示 Content-Type application/json

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()

这对我适用于Python2.7,Ubuntu 14.04

This works for me on Python2.7, Ubuntu 14.04

如果您遇到任何问题,请告诉我:-)

Let me know if you face any issues :-)

这篇关于使用浏览器的XHR日志重新创建AJAX请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆