好用的模块
import requests
#1、发get请求
# url = 'http://api.nnzhp.cn/api/user/stu_info'
# data = {'stu_name':'小黑'} #请求数据
# req = requests.get(url,params=data) #发get请求
# print(req.json()) #字典
# print(req.text) #string,json串
#返回的都是什么
#返回的类型是什么
#中文的好使吗
#2 、发post请求
# url = 'http://api.nnzhp.cn/api/user/login'
# data = {'username':'niuhanyang','passwd':'aA123456'} #请求数据
# req = requests.post(url,data) #发送post请求
# print(req.json())
#3、入参是json类型的
# import random
# phone=random.randint(10000000000,99999999999)
# url='http://api.nnzhp.cn/api/user/add_stu'
# data = {
# "name":"小1",
# "grade":"天蝎座",
# "phone":phone,
# "sex":"男",
# "age":28,
# "addr":"河南省济源市北海大道32号"
# }
# req = requests.post(url,json=data)
# print(req.json())
# 4、添加cookie
# url = 'http://api.nnzhp.cn/api/user/gold_add'
# data = {'stu_id':468,'gold':10000}
# djl = {'niuhanyang':'337ca4cc825302b3a8791ac7f9dc4bc6'}
# req = requests.post(url,data,cookies=djl)
# print(req.json())
#5、添加header
# url = 'http://api.nnzhp.cn/api/user/all_stu'
# header = {
# 'Referer':'http://api.nnzhp.cn/'
# }
# req = requests.get(url,headers=header)
# print(req.json())
#6、上传文件
# url= 'http://api.nnzhp.cn/api/file/file_upload'
# data = {
# 'file':open(r'C:UsersjniuhanyangDesktop图6bd9026dt935575932465&690.jpg','rb')
# }
# req= requests.post(url,files=data)
# print(req.json())
#7、下载文件
url = 'http://up.mcyt.net/?down/46779.mp3'
req = requests.get(url)
fw = open('aaa.mp3','wb')
fw.
内置函数
#zip
l1 = ['a','b','c','e','f','g']
l2 = [1,2,3]
l3=['A','B','C']
L4=['牛','牛','niu']
#zip,就是把俩list,合并到一起,如果想同时循环2个list的时候,
for a,b,c,d in zip(l1,l2,l3,L4):
print(a,b,c,d)
# 下载一个文件,或者下个图片
#map #她是帮你循环调用函数的
# def my(num):
# return str(num)
# lis = [1,2,3,4,5,6,7,8,9]
# res = list(map(my,lis))
# print(res)
# new_lis = []
# for i in lis:
# new_lis.append(my(i))
#filter 也是帮你循环调用函数的,过滤
def even(num):
if num%2==0:
return True
return False
lis = [1,2,3,4,5,6,7,8,9]
res = filter(even,lis)
print('filter..',list(res)) #filter只保留,返回为真的数据
res2 = map(even,lis)
print('map..',list(res2)) #map是帮你循环调用函数,这个函数返回就保存什么。
#结果是什么,它和map和的结果有什么区别
网络编辑
from urllib import request,parse
# url = 'http://www.nnzhp.cn'
# req = request.urlopen(url) #打开一个url,发get请求
# content = req.read().decode() #获取返回结果
# fw = open('baidu.html','w',encoding='utf-8')
# fw.write(content)
import json
# url='http://api.nnzhp.cn/api/user/stu_info?stu_name=xiaohei'
# req = request.urlopen(url) #打开一个url,发get请求
# content = req.read().decode() #获取返回结果
# res_dic = json.loads(content) #返回的结果转成字典
# if res_dic.get('error_code') == 0:
# print('测试通过')
# else:
# print('测试失败',res_dic)
url = 'http://api.nnzhp.cn/api/user/login'
data = {
'username':'admin',
'passwd':'aA123456'
} #请求数据
data = parse.urlencode(data) #urlencode,自动给你拼好参数
# xx=xx&xx=11
req = request.urlopen(url,data.encode()) #发post请求
print(req.read().decode())
# 网络爬虫,从其他的网站上,获取一些有用的内容。