DarkTime
  • README
  • SpringBoot
    • spring boot 运维
    • Spring Boot 部署war包
    • springboot搭建
    • spring boot 读取配置文件
    • 简单总结
    • spring配置文件
    • spring boot Configuration
    • spring boot 配置文件
    • spring boot 配置mybatis
  • MacAwesome
    • markdown使用
    • MAC APP Awesome
    • [markdown使用](/MacAwesome/SUMMARY.md)
    • chrome
    • intellij idea
    • MacAwesome
    • VS Code 的使用
    • MAC Shell命令
  • database
    • druid使用
  • 框架
    • 项目拆析
    • 各种框架和工具
  • docker
    • kubernetes
    • docker
    • docker 常用镜像
  • 效率工具
    • 解决dns污染导致域名解析失败
    • sonarqube 相关配置
    • Iterm2 使用
    • gitbook
    • github awesome github资源推荐
    • playground 在线试用平台汇总
    • linux中的office
    • linux screen 工具
    • 简单Mock服务(moco)
    • npm
    • Visual Studio Code 的使用
    • 配置开发环境
    • homebrew的使用
    • 汇总
  • tomcat
    • tomcat目录规范
  • code_snippets
  • 专题
    • RESTful API
    • serveless服务
    • 搭建私有云主机 折腾记
    • 开发中的各种疑难杂症问题
    • spring 最佳实践
    • LLM 大语言模型
    • notelive
      • 文章框架
      • notelive vue版本开发
      • notelive 开发 札记
    • webrtc技术分析
    • 反向代理
    • spring-cloud
      • spring boot admin 监控服务
      • Spring Cloud 整理汇总
  • python
    • python 学习
    • Python 修饰器的一些小细节
  • 云主机
    • aliyun 主机的种种
  • maven
    • maven使用
    • maven项目增加编译版本号 buildnumber-maven-plugin
    • 仓库
  • java
    • java 开发常用工具类
    • java
    • apache commons pool 对象连接池
  • 大数据
    • kafka
    • gobblin
    • sqoop 简介及使用
    • hbase
    • gobblin
    • sqoop源码解析
    • hadoop map reduce
    • 大数据 学习札记
  • 脚本
    • python
      • 批量请求url 解析json数据
    • js
      • sheetjs-js读取excel
    • shell
      • 自动生成bitbook的summary文件
      • linux/mac 实用脚本
      • 自动创建tomcat项目脚本
      • 批量处理文件内容脚本
  • nginx
    • nginx
    • ngix 文件浏览器 文件服务器
  • linux
    • 群晖nas札记
    • ftp
    • linux 运维
    • 常用命令
    • linux
    • mysqldump脚本
    • 代理
    • 简易灰度部署脚本 不使用jenkins的纯shell方式
    • shell脚本
    • 附加文档
  • mysql
    • sql
  • 游戏开发
    • Unity 2020 学习笔记
  • 学习笔记
    • centos常用环境安装
    • gradle 学习
    • 建站经历
    • python
      • 爬虫教程
    • 如何解决百度爬虫无法爬取搭建在Github上的个人博客的问题? - 知乎
    • baas
      • 在本地部署Parse Server
    • mysql学习标记
    • java code snippets
    • 非Spring Boot Web项目 注册节点到Eureka Server并提供服务
    • kotlin
      • Kotlin 学习札记
    • spring cloud
    • vim配置
    • 前端
      • 开发PWA应用
  • jenkins
    • jenkins配置备份
    • gitlab触发Jenkins 自动构建
    • 安装与使用
  • npm
    • npm 使用
  • git
    • ignore
    • git使用总结
    • git配置多个远程仓库
  • 前端
    • swig
    • 解决跨域请求问题
    • angularjs 学习
    • scriptbot的前端开发经验总结
    • 各种资源
    • 一些有用的js代码
Powered by GitBook
On this page

Was this helpful?

  1. 脚本
  2. python

批量请求url 解析json数据

# -*- coding: UTF-8 -*-
#!/usr/bin/python

import urllib2
import urllib
import json

def byteify(input):
    if isinstance(input, dict):
        return {byteify(key): byteify(value) for key, value in input.iteritems()}
    elif isinstance(input, list):
        return [byteify(element) for element in input]
    elif isinstance(input, unicode):
        return input.encode('utf-8')
    else:
        return input


result="uid price\n"

def request(uid,cid):
    uid=str(uid)
    cid=str(cid)
    print "uid:",uid,"cid:",cid
    dataRes = urllib.urlopen('http://host/request&contractId='+cid+'&iamsid='+uid)
    #通过urllib模块中的urlopen的方法打开url
    data = dataRes.read()
    #通过read方法获取返回数据
    # print "url返回的json数据:",data
    #打印返回信息
    dataJSON = json.loads(data)
    dataJSON=dataJSON["data"]

    for price in dataJSON:
        if price["code"] == "kvm" :
                print uid,price["priceDate"]
                global result
                result=result+"\n"+uid+" "+price["priceDate"]

# param=[(70304362,157536),(70304362,157536)]
# for p in param:
#     request(p[0],p[1])


with open('uids.txt') as f:
    for line in f:
        uidcid=line.split()
        request(uidcid[0],uidcid[1])

#将返回的json格式的数据转化为python对象,json数据转化成了python中的字典,按照字典方法读取数据
# print "python的字典数据:",dataJSON
# print "字典中的data数据",dataJSON["data"]
# print "lists列表的数据",dataJSON["data"]["lists"][0]
#lists里面的数据是一个列表(按照序列编号来查看数据)
# print weatherJSON["data"]["lists"][0]["SongName"]
#lists的0号数据是一个字典,按照字典方法查看数据


#dumps()默认中文伟ascii编码格式,ensure_ascii默认为Ture
#禁用ascii编码格式,返回Unicode字符串
# dataJson=json.dumps(dataJSON,ensure_ascii=False)

with open("export.txt","w") as f:
    f.write(result.encode("utf-8"))

uids.txt 参数文件示例

```txt uids.txt 参数文件 13368 156622 79682 156011 263190 155071

## 讲解

`dataJSON = json.loads(data)`读取的文件是unicode编码,如果要输出utf8格式的需要如下方式:

```python
#调用dumps方法输出字符串
dataStr=json.dumps(dataJSON,ensure_ascii=False)
#以utf8格式编码
print dataStr.encode("utf-8")

参考资料

PreviouspythonNextjs

Last updated 6 years ago

Was this helpful?

python爬虫 如何解析json文件 json文件的解析提取和jsonpath的应用 - CSDN博客
python 数据提取之JSON与JsonPATH - AlamZ - 博客园