python模拟ajax请求,获得ajax请求响应,pythonajax,有些网站的数据需要通过a
python模拟ajax请求,获得ajax请求响应,pythonajax,有些网站的数据需要通过a
有些网站的数据需要通过ajax来请求获得,ajax请求在发起请求之前必须设置必要的http头,还需要将请求数据封装为json格式。
如下代码片段:
def request_ajax_data(url,data,referer=None,**headers): req = urllib2.Request(url) req.add_header('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8') req.add_header('X-Requested-With','XMLHttpRequest') req.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116') if referer: req.add_header('Referer',referer) if headers: for k in headers.keys(): req.add_header(k,headers[k]) params = urllib.urlencode(data) response = urllib2.urlopen(req, params) jsonText = response.read() return json.loads(jsonText)
用法示例:
ajaxRequestBody = {"blogId":blogId,"postId":entryId,"blogApp":blogApp,"blogUserGuid":blogUserId}ajaxResponse = request_ajax_url('http://byrx.net/fakeAjax',ajaxRequestBody)
相关内容
- python修改mp3 专辑,作者等信息,pythonmp3,使用python修改
- python合并文件代码,python合并代码,from glob im
- python计算过去多长时间,python计算多长时间,python的日期
- python将微软InfoPath文件转换为csv文件,,InfoPath是xml
- python中使用enum的最佳实践,enum最佳实践,python中默认没
- python setattr使用示例,pythonsetattr示例,python中可以使用
- python 带可选参数的命令行,python可选,#!/usr/bin/e
- python检查文件是否存在,以及路径是否为文件,python路
- python检查list是否为空的方法,python检查list,python有两种
- python在创建文件之前检查目录是否存在,若不存在则创
评论关闭