Python发送http请求解析返回json的实例

作者:tanghaiyu777 时间:2022-04-11 03:38:28 

python发起http请求,并解析返回的json字符串的小demo,方便以后用到。


#! /usr/bin/env python
 # -*- coding:gbk -*-

import os
 import sys
 import json
 import urllib
 import urllib2

if __name__ == "__main__":
   query_file = sys.argv[1]
   query_index = 0
   with open(query_file, 'r') as fp:
     for line in fp:
       query = line.rstrip()
       query_index = query_index + 1
       query_gbk = query
       query = query.decode('gbk', 'ignore').encode('utf8', 'ignore')
       url = 'http://10.42.141.12:8089/adrender?query=%s&ad_num=3&srcid=101'\\
          '&ip=172.22.182.55&baiduid=61ABB404320C72436EB6B8352DFBB388:FG=1' % (query)
       req = urllib2.urlopen(url)
       page = req.read()
       ddict = json.loads(page)
       expid = ddict['expid']
       sid = ddict['sid']
       ad_num = ddict['response_adnum']
       for i in range(0, ad_num):
         output_html = '%s-%d.html' % (query_gbk, i)
         output = open(output_html, 'w')
         ad = ddict['response_ads'][i].encode('utf8', 'ignore')
         output.write('<html>\')
         output.write('<head>\  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">\<head>\')
         output.write("%s" % (ad))
         output.write('\</html>\')
         output.close()

来源:https://blog.csdn.net/tanghaiyu777/article/details/55270992

标签:http,请求,返回,json
0
投稿

猜你喜欢

  • Python读取YAML文件过程详解

    2021-04-21 07:12:25
  • asp日期转换成汉字格式程序

    2008-07-08 18:19:00
  • thinkphp5加layui实现图片上传功能(带图片预览)

    2023-06-13 01:09:45
  • 交互设计实用指南系列(1) – “有效性”之“操作入口明确”

    2009-12-11 18:42:00
  • 防止网站被采集的理论分析以及十条方法对策第1/2页

    2011-03-29 10:38:00
  • SQL SERVER 日志已满的处理方法

    2010-07-31 13:32:00
  • Highcharts 图表中图例显示状态存储的功能设计详解

    2023-05-30 02:01:09
  • asp使用Application来统计在线人数方法

    2007-08-13 12:43:00
  • Web标准下该如何“插入”图像

    2008-03-17 13:01:00
  • 老版本PHP转义Json里的特殊字符的函数

    2023-11-06 02:32:18
  • python 实现数据库中数据添加、查询与更新的示例代码

    2023-10-08 04:09:51
  • 自己写的一个PJBlog中可以双击输入验证码的修改

    2009-05-17 10:51:00
  • 在Python中操作文件之seek()方法的使用教程

    2023-08-01 14:58:01
  • 常用python数据类型转换函数总结

    2023-07-27 23:07:16
  • js自定义快捷,IE,FF有良好的支持

    2007-08-25 17:34:00
  • SQL0290N表空间状态问题:停顿的独占处理

    2008-12-26 17:24:00
  • 2008年个性设计挂历参考

    2007-12-29 20:30:00
  • Python+Pygame制作"长沙版"大富翁

    2023-10-05 06:53:08
  • php实现微信支付之企业付款

    2023-11-24 02:06:43
  • 关于Python 中的时间处理包datetime和arrow的方法详解

    2023-11-16 12:18:48
  • asp之家 网络编程 m.aspxhome.com