对json字符串与python字符串的不同之处详解
作者:子灬丶逾 时间:2023-11-22 04:35:39
API的应用通常会处理json数据,刚好今天看到了json字符串和python字符串的区别,放一段代码,区别一下子就看出来,的确json 库为处理Json 数据提供了不少的便利。
import json
jsonString = '{"arrayOfNums":[{"number":0},{"number":1},{"number":2}],"arrayOfFruits":[{"fruit":"apple"},{"fruit":"banana"},{"fruit":"pear"}]}'
jsonObj = json.loads(jsonString)
print(jsonObj.get("arrayOfNums"))
print(jsonObj.get("arrayOfNums")[0].get('number'))
#json 是一个字符串形式的。 没有get方法
#python 字符串有get方法 便于处理 json里面的数据
下面是一段通过ip地址查询地理位置信息的代码,也贴上去,接口是免费的
import json
from urllib.request import urlopen
def getCountry(ipAddress):
response = urlopen("http://freegeoip.net/json/"+ipAddress).read().decode('utf-8')
responseJson = json.loads(response)
print(responseJson)
return responseJson.get("country_code")
print(getCountry("50.78.253.58"))
(代码来自python网络数据采集)
刚好看到,在貼个库的用法上去,urllib.request.urltrieve 可以根据链接把文件下载下来,上代码好理解一些
from urllib.request import urlretrieve
from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen('http://www.pythonscraping.com')
bs4 = BeautifulSoup(html,'xml')
imageLocation = bs4.find("a",{"id":"logo"}).find("img")['src']
urlretrieve(imageLocation,"logo.jpg") #urlretrieve 根据下载链接 可以把文件下载下来
#把logo下载在当前目录,名字叫logo.jpg
来源:https://blog.csdn.net/qq_37129433/article/details/76007479
标签:json,python,字符串
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Yolov5多边形标签和JSON数据格式转换
2023-11-06 17:31:24
![](https://img.aspxhome.com/file/2023/4/117944_0s.jpg)
zend framework文件上传功能实例代码
2024-05-05 09:17:17
asp如何计算下载一个文件需要多长时间?
2009-11-25 20:17:00
关于JSON以及JSON在PHP中的应用技巧
2023-11-16 00:03:38
PHP屏蔽蜘蛛访问代码及常用搜索引擎的HTTP_USER_AGENT
2023-09-08 08:07:37
![](https://img.aspxhome.com/file/2023/8/55628_0s.jpg)
Node.js原理阻塞和EventEmitter及其继承的运用实战
2024-05-05 09:21:33
![](https://img.aspxhome.com/file/2023/3/129343_0s.png)
python修改字典键(key)的方法
2023-04-16 20:26:57
![](https://img.aspxhome.com/file/2023/3/107663_0s.jpg)
利用Python判断文件的几种方法及其优劣对比
2022-07-03 06:20:02
Python3 shelve对象持久存储原理详解
2022-06-30 13:43:39
![](https://img.aspxhome.com/file/2023/2/86542_0s.png)
国外404错误页面的创意设计欣赏
2007-08-19 15:44:00
![](https://img.aspxhome.com/file/UploadPic/20078/19/2007819154957564s.jpg)
SqlServer中的日期与时间函数
2011-11-03 17:12:34
Dreamweaver技巧十二招
2009-07-05 18:53:00
JAVA简单链接Oracle数据库 注册和登陆功能的实现代码
2024-01-23 13:15:12
在Python的Tornado框架中实现简单的在线代理的教程
2021-12-31 08:51:39
基于pytorch 预训练的词向量用法详解
2021-04-04 21:35:37
Python实例解析图像形态学运算技术
2021-04-14 10:34:24
![](https://img.aspxhome.com/file/2023/3/135143_0s.png)
asp好用的函数集分享
2011-09-13 12:49:07
百度地图API之本地搜索与范围搜索
2023-08-23 17:24:38
![](https://img.aspxhome.com/file/2023/6/56176_0s.jpg)
全民学编程之 Hello World
2023-03-13 20:48:37
PHP版微信小店接口开发实例
2023-11-10 11:56:06