zoukankan      html  css  js  c++  java
  • 【Python学习】网络编程

    一、什么是网络编程

      使用Python进行网络编程,就是通过Python打开一个网站,或者请求一个http接口。可以通过标准模块urllib实现,也可以通过更简单易用的第三方模块requests实现。

    二、urllib

    复制代码
     1 # 1、没有入参的get请求
     2 import json
     3 from urllib import request,parse
     4 url = 'http://www.baidu.com'
     5 req = request.urlopen(url)     # 打开一个url,发get请求
     6 content = req.read().decode()  # 获取返回结果,返回结果是bytes类型需要加decode()
     7 fw = open('baidu.html','w',encoding='utf-8')  # 创建一个html文件
     8 fw.write(content)              # 将返回的内容写入html文件
     9 
    10 # 2、有入参的get请求
    11 url = 'http://api.nnzhp.cn/api/user/stu_info?stu_name=小黑'
    12 req = request.urlopen(url)     # 打开一个url,发get请求
    13 content = req.read().decode()  # 获取返回结果,返回结果是bytes类型需要加decode()
    14 res_dic = json.loads(content)  # 将返回的json串转换为字典
    15 if res_dic.get('error_code') == 0:  # 根据返回的error_code判断测试是否通过
    16     print('测试通过')
    17 else:
    18     print('测试不通过')
    19 
    20 # 3、post请求
    21 url = 'http://api.nnzhp.cn/api/user/login'
    22 data = {'username':'niuhanyang','passwd':'aA123456'}  # 请求数据
    23 data = parse.urlencode(data)  # 自动拼好参数username=niuhanyang&passwd=aA123456
    24 req = request.urlopen(url,data.encode()) # 发post请求,请求数据需要转成bytes类型
    25 print(req.read().decode())    # 获取返回结果,返回结果是bytes类型需要加decode()
    复制代码

    三、requests

    复制代码
     1 # 1、发get请求
     2 import requests
     3 url = 'http://api.nnzhp.cn/api/user/stu_info'
     4 data = {'stu_name':'小黑'}  # 请求数据
     5 req = requests.get(url,params=data)  # 发get请求
     6 print(req.json())  # 字典
     7 print(req.text)    # string类型json串
     8 
     9 # 2、发post请求
    10 url = 'http://api.nnzhp.cn/api/user/login'
    11 data = {'username':'niuhanyang','passwd':'aA123456'}  # 请求数据
    12 req = requests.post(url,data)  # 发post请求
    13 print(req.json())  # 字典
    14 print(req.text)    # string类型json串
    15 
    16 # 3、入参是json类型的post请求
    17 import random
    18 phone = random.randint(10000000000,99999999999)
    19 url = 'http://api.nnzhp.cn/api/user/add_stu'
    20 data = {
    21     "name":"小A",
    22     "grade":"天蝎座",
    23     "phone":phone,
    24     "sex":"男",
    25     "age":28,
    26     "addr":"河南省济源市北海大道32号"
    27   }  # 请求数据
    28 req = requests.post(url,json=data)  # 发post请求
    29 print(req.json())  # 字典
    30 print(req.text)    # string类型json串
    31 
    32 # 4、post请求,添加cookie
    33 url = 'http://api.nnzhp.cn/api/user/gold_add'
    34 data = {'stu_id':467,'gold':'1000'}
    35 cookie = {'niuhanyang':'337ca4cc825302b3a8791ac7f9dc4bc6'}
    36 req = requests.post(url,data,cookies=cookie)  # 发post请求
    37 print(req.json())  # 字典
    38 
    39 # 5、post请求,添加header
    40 url = 'http://api.nnzhp.cn/api/user/all_stu'
    41 header = {'Referer':'http://api.nnzhp.cn/'}
    42 req = requests.get(url,headers=header)
    43 print(req.json())  # 字典
    44 
    45 # 6、上传文件
    46 url = 'http://api.nnzhp.cn/api/file/file_upload'
    47 # data = {'file':open('baidu.html',encoding='utf-8')} # 文件中有中文的话要用encoding='utf-8'
    48 data = {'file':open('QQ截图20180512164822.jpg','rb')} # 打开图片要用rb模式
    49 req = requests.post(url,files=data)
    50 print(req.json()) # 字典
    51 
    52 # 7、下载图片/网页/文件
    53 url = 'http://www.nnzhp.cn/wp-content/uploads/2018/01/soup.jpg'  # 图片地址
    54 # url = 'http://www.nnzhp.cn/archives/140'   # 网页地址
    55 # url ='http://up.mcyt.net/?down/46779.mp3'  # mp3地址
    56 req = requests.get(url)  # 发送get请求
    57 # print(req.content)     #content就是返回的二进制文件内容
    58 fw = open('s.jpg','wb')     # 'wb'二进制写模式,下载图片
    59 # fw = open('s.html','wb')    # 下载网页
    60 # fw = open('song.mp3','wb')  # 下载音乐
    61 fw.write(req.content)
    作者:gtea 博客地址:https://www.cnblogs.com/gtea
  • 相关阅读:
    台州 OJ 3847 Mowing the Lawn 线性DP 单调队列
    洛谷 OJ P1417 烹调方案 01背包
    快速幂取模
    台州 OJ 2649 More is better 并查集
    UVa 1640
    UVa 11971
    UVa 10900
    UVa 11346
    UVa 10288
    UVa 1639
  • 原文地址:https://www.cnblogs.com/gtea/p/12715654.html
Copyright © 2011-2022 走看看