Python弄明白以下类库:
1、json:
JSON (JavaScript Object Notation) 是一种轻量级的数据交换格式。Python3 中可以使用 json 模块来对 JSON 数据进行编解码,它主要提供了四个方法:
dumps
、dump
、loads
、load
。
json.dump()
json.dumps()
json.load()
json.loads()
2、requests
参考学习了:
import requests
import json
# 打开一个网页,get,post,pull,delete,head,options
url = "https://www.baidu.com"
response = requests.get(url)
# 设置返回的类型编码,一般都是utf-8或gbk,否则中文会出现乱码,此处非必需
response.encoding = 'utf-8'
# 查看返回的代码,正常为:200,404是找不到网页
print(response.status_code)
# 查看返回的类型:<class 'requests.models.Response'>
print(type(response))
# 查看返回的类型编码
print(response.encoding)
# 查看内容:用于获取文本及源代码的数据(将数据转变为字符串)
print(response.text)
# 查看内容:用于获取图片及音视频等数据(将数据转变为二进制)
print(response.content)
#获得响应头内容
print(response.headers)
print(response.headers['content-type'])
# 将内容追加写入文件里
file = open('temp.txt', 'a+', encoding='utf-8')
file.write(response.text)
file.close()
# 下载一个图片
imgage_url = 'http://c.biancheng.net/uploads/allimg/191009/1-191009162420B7.gif'
response = requests.get(imgage_url)
if response.status_code == 200:
imagefile = open('imageDemo.jpg', 'wb')
imagefile.write(response.content)
imagefile.close()
print("下载图片成功!")
else:
print("下载图片出错。")
# Requests 参数相关 两种方式,一种是URL直接用?action=quey&name=aaa方式
url = 'https://www.baidu.com'
paras = {'action': 'quey', 'name': 'aaa'}
response = requests.get(url, params=paras)
print(response.url)
3、base64
url = "https://www.cnblogs.com/songzhixue/"
bytes_url = url.encode("utf-8")
str_url = base64.b64encode(bytes_url) # 被编码的参数必须是二进制数据
print(str_url)
b'aHR0cHM6Ly93d3cuY25ibG9ncy5jb20vc29uZ3poaXh1ZS8='
url = "https://www.cnblogs.com/songzhixue/"
bytes_url = url.encode("utf-8")
str_url = base64.b64encode(bytes_url) # 被编码的参数必须是二进制数据
print(str_url)
b'aHR0cHM6Ly93d3cuY25ibG9ncy5jb20vc29uZ3poaXh1ZS8='
理工男(作者)自己的公众号:
一个理工男的成长之路,如果你是理工男,带你“不正经”;如果你想了解理工男,带你认识理工男,他们是生活百事通,他们的生活简约而不简单。
不正经的原则:合规合法,信息保真能看懂,字越少事越大,学到真东西享受真实惠,有兴趣加入我一起“不正经”。