zoukankan      html  css  js  c++  java
  • Python学习笔记十九_网络编程

      Python操作网络,就是打开一个网站,或者请求一个http接口。可以通过标准模块urllib实现,也可以通过更简单易用的第三方模块requests实现。下面来分别看下通过这俩模块如何实现

    一、urllib

    urllib模块是一个标准模块,直接import urllib即可,在python3里面只有urllib模块,在python2里面有urllib和urllib2模块。

    req =request.urlopen(url,data.encode())

    req.read().decode()

    import json
    from urllib import request,parse
    #没有入参的get请求
    url = 'http://www.baidu.com'
    req = request.urlopen(url)#打开一个url,发get请求
    content = req.read().decode()#获取返回结果,bytes需要decode
    fw = open('baidu.html','w',encoding='utf-8')#将文件存成html的格式,可以用浏览器打开
    fw.write(content)#最初级的网络爬虫,从其他的网站上,获取一些有用的内容
    #有入参的get请求
    url='http://127.0.0.1/api/user/stu_info'
    data = {'stu_name':'Amy'}#请求数据
    data = parse.urlencode(data)#把请求参数变成stu_name=Amy
    req=request.urlopen(url+'?'+data)#发get请求
    content = req.read().decode()#获取到的是json串
    res_dic = json.loads(content)#将json串转为字典
    print(res_dic)
    #post请求
    url = 'http://127.0.0.1/api/user/login'
    data = {'username': 'Amy','passwd':'aA123456'}#请求数据
    data=parse.urlencode(data)#urlencode,自动给你拼好参数username=Amy&passwd=aA123456
    req =request.urlopen(url,data.encode())#发post请求,请求数据(入参)需要转成bytes
    print(req.read().decode())#获取返回结果,bytes需要decode

    二、requests

      使用urllib去请求网站或者接口,很麻烦,传参需要是bytes类型,得先encode()编码。返回数据也是bytes类型,还得decode()解码。想要把返回结果拿来使用还需要json.loads一下才能转为字典。

      requeste这个第三方模块就比较方便,它需要安装,pip install requests即可。

    import requests
    
    #1、发get请求,params=data
    url ='http://127.0.0.1/api/user/stu_info'
    data ={'stu_name':'Amy'}#请求数据,可以是中文,urllib中不能是中文
    req = requests.get(url,params=data)#发get请求
    print(req.json())#直接返回字典,不用json.load,也不用decode
    print(req.text)#返回的是字符串
    
    #2、发post请求,data
    url = 'http://127.0.0.1/api/user/login'
    data = {'username': 'Amy','passwd':'aA123456'}
    req = requests.post(url,data)
    print(req.json())
    
    #3、入参是json类型,json=data
    url = 'http://127.0.0.1/api/user/add_stu'
    data = {
        "name":"Amy",
        "grade":"天蝎座",
        "phone":'18611222211',
        "sex":"",
        "age":28,
        "addr":"北京"
      }
    req = requests.post(url,json = data)
    print(req.json)
    
    #4、添加cookie,cookies =cookie
    url = 'http://127.0.0.1/api/user/gold_add'
    data = {'stu_id':'500','gold':'100'}
    cookie = {'Amy':'44df3680451eb4ed96ef9e3a21f1165f'}
    req = requests.post(url,data,cookies=cookie)
    print(req.json())
    
    # 5、添加header,headers = header
    url = 'http://127.0.0.1/api/user/all_stu'
    header = {'Referer':'http://api.nnzhp.cn/'}
    req = requests.get(url,headers=header)
    print(req.json())
    
    #6、上传文件,files =data
    url = 'http://127.0.0.1/api/file/file_upload'
    data = {
        'file':open('笔记',encoding='utf-8')#传文件
        # 'file':open('Penguins.jpg','rb')#传图片,‘rb'二进制读模式
        # 'file':open('dr.xlsx','rb')#传excel
    }
    req = requests.post(url,files =data)
    print(req.json())
    
    #7、下载文件,或者下载图片,找到文件对应的url
    url = 'http://www.nnzhp.cn/wp-content/uploads/2018/01/soup.jpg'#下载图片
    # url = 'http://www.nnzhp.cn/archives/140'#下载网页
    # url ='http://up.mcyt.net/?down/46779.mp3'#下载音乐
    req = requests.get(url)
    # print(req.content)#content就是返回的二进制文件
    fw = open('s.jpg','wb')#'wb'二进制写模式,下载图片
    # fw = open('s.html','wb')#下载网页
    # fw = open('song.mp3','wb')#下载音乐
    fw.write(req.content)
  • 相关阅读:
    2019-2020-2 网络对抗技术 20175232 司浩楠 Exp2 后门原理与实践
    2019-2020-2 网络对抗技术 20175232 司浩楠 Exp1 PC平台逆向破解
    2019-2020-1 20175232 20175233 《信息安全系统设计基础》实验五 通讯协议设计
    Linux中Qt的安装
    面向对象编程与面向过程编程的区别与联系
    Web服务器端程序的实现
    Web服务器文件传输程序客户端程序实现
    Web服务器实现文件传输程序设计
    屏蔽信号的多路选择I/O
    浅谈HTTP请求与响应
  • 原文地址:https://www.cnblogs.com/dongrui624/p/9054145.html
Copyright © 2011-2022 走看看