Python3 获取ajax 返回内容

使用工具:浏览器chrome(或者firefox);python3

什么样的页面是使用了ajax技术?留给你回答..

打开豆瓣页面,按F12,选择network--->XHR (XMLHttpRequest )进行筛选。因为ajax就是async javascript and xml缩写,既通过原生的XHR对象发出HTTP请求,得到服务器返回的数据后,再进行处理。

http请求内容主要包括:request URL,request menthod,request header,response header,parpameter。

Python3 获取ajax 返回内容_第1张图片

其中method有post和get两种方法。区别就是get,那么request url就是这个链接的返回值,使用json格式输出;如果是post,则request url返回一般是空,ajax需要根据from data判断后才返回信息。

这里详细说下python模拟post方法的获取response:

    需要的参数包括:request url,parameter (from data)

伪代码如下:

    import requests

    from lxml.etree import fromstring

    import json

    API_url='http://www.xxxxxxxxxxxxxx_ajax.htm'  ##可能url并不会包含ajax关键字 

    def get_stores_info(self,page):
        data={'pages':page} ##需要尝试,可能有些参数是冗余的
        response=requests.post(self.API_url,data=data)
        return response.json()
##定义function 执行并调用:
     def run(self):
        page=100
        for commodityid in range(page):
                data=self.get_stores_info(page)

                print(data)

##




你可能感兴趣的:(python)