网络编程


 

1 from urllib.request import urlopen
2 from urllib.parse import urlencode
# get 请求
1 url = 'http://www.cnblogs.com/Printwj/'
2 res = urlopen(url) # 发送get请求
3 print(res.read().decode())  # 返回网页的源代码
# post 请求
1 url = 'http://127.0.0.1:8989/login'
2 data = {'username':'admin','password':'123456'}
3 print(urlencode(data)) # 字典转换成字符串
4 #k=v&x=l
5 res = urlopen(url,urlencode(data).encode()) # post请求
6 print(res.read().decode())

post请求的结果:

 

 接下来再说一下request模import requests

1 # get 请求
2 url1 = 'http://xxx.xxx.xxx.xxx/api/user/stu_info'
3 res = requests.get(url1,params={'stu_name':'小黑'}) # 发送get请求
4 print(res.json())  # .json直接把返回结果转成字典
1 # post 请求
2 url2 ='http://xxx.xxx.xxx.xxx/api/user/login'
3 data2 = {'username':'admin','password':'123456'}
4 res = requests.post(url2,data=data2)
5 print(res.json()) # .json返回的是字典
# json传参
url3 ='http://xxx.xxx.xxx.xxx/api/user/add_stu'
data = {
    "name":"requests_name",
    "grade":"天蝎座",
    "phone":"18888888888",
    "sex":"",
    "age":18,
    "addr":"杭州市西湖区"
}
res = requests.post(url3,json=data)
print(res.json()) # .json返回的是字典
# 传cookie
url4 = 'http://xxx.xxx.xxx.xxx/api/user/gold_add'
data = {'stu_id':15,'gold':200}
cookie = {'admin':'abd9a0995f4696e1a60133220b32037a'}
res = requests.post(url4,data=data,cookies=cookie)
print((res.json())) # .json返回的是字典
# 传header
url5 = 'http://xxx.xxx.xxx.xxx/api/user/all_stu'
header = {'Referer':'http://api.nnzhp.cn/'}
res = requests.get(url5,headers = header)
print(res.json()) # .json返回的是字典

url6 = 'http://www.cnblogs.com/Rrintwj/'
res = requests.get(url6)
print(res.json()) # .json返回的是字典
print(res.text) # 返回的都是字符串
# 下载MP3
url7 = 'http://qiniuuwmp3.changba.com/1084511584.mp3'
res = requests.get(url7)
# print(res.text)
# print(res.content)  # 返回的就是2进制的
with open('魔鬼中的天使.mp3','wb') as fw: #w/r +个b就是以2进制的方式读/写
    fw.write(res.content)
# 下载图片
url8 = 'https://aliimg.changba.com/cache/photo/855e5493-f018-44db-8892-c8660649327b_640_640.jpg'
res = requests.get(url8,verify=False)   # verify=False 如果是https的话加上这个
print(res.text)
print(res.content)  # 返回的就是2进制的
with open('tu.jpg','wb') as fw: #w/r +个b就是以2进制的方式读/写
    fw.write(res.content)

# print(res.json())   # 必须返回的是json才可以用
# print(res.text)     # 下载文件的话text就不行了
# print(res.content)  # 用来下载文件用的,返回的是二进制
# print(res.headers)  # 获取到返回的所有header
# print(res.cookies)  # 获取到返回的所有cookie
# 上传文件
url9 = 'http://xxx.xxx.xxx.xxx/api/file/file_upload'
data = {'file':open('魔鬼中的天使.mp3','rb')}
res = requests.post(url9,files=data)
print(res.json())

异常处理


在运行代码的时候经常会出现异常,通常这个时候,后面的代码不会继续执行,那么如果想让它继续执行下去,我们就需要对异常进行处理!
下面简单的写一个例子:
money = 1000
num = input('plese enter a num:')
try:
    num = float(num)
    res = money/num
except Exception as e:  # e 指具体的报错信息
    print('出现异常了,错误信息:\n%s'%e)
else:   # 没有出现异常
    money-=num
    print(res)
    print('你的余额未%s'%money)
finally:
    print('Finally')

面向对象编程


# 面向过程

# 面向对象

class Person: # 类,类名都以大写字母开头
    country = 'China'   # 类变量
    eyes = 2
    ears = 2
    mouth = 1
    def __init__(self,name,sex=''): # 构造函数
        # 函数就是这个类在初始化的时候执行的
        self.name = name    # 绑定属性
        # 实例变量,成员变量
    def cry(self):
        print('%s哇哇哇'%self.name)

tmp = Person('小黑','') # 实例化——类名+()
print(tmp.ears)
tmp.cry()   # 调用它下面的方法
print(tmp.ears)
tmp.ears = 8    # 可以对其进行修改
print(tmp.ears)
tmp.money = 10000 # 可以在实例化外面添加属性

 

后续补充ing...