#网络爬虫,从其他的网站上,获取一些有用的内容,存入自己的数据库,然后再展示在指定的位置。 #urllib是python自带的模块 1.urllib模块做网络爬虫,爬取网页:
from urllib import request,parse #导入urllib模块 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) #将baidu.html保存在了本地 2.urllib模块访问接口,get请求:
import json url='http://api.**xx.cn/api/user/stu_info?stu_name=xxxx' 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) 3.urllib模块访问接口,post请求: url = 'http://api.xx**.cn/api/user/login' data = { 'username':'admin', 'passwd':'xxxx126' } #请求数据 data = parse.urlencode(data) #urlencode,自动给你拼好参数 # xx=xx&xx=11 req = request.urlopen(url,data.encode()) #发post请求 print(req.read().decode())