美文网首页
网络操作操作GET,POST

网络操作操作GET,POST

作者: wangyu2488 | 来源:发表于2019-12-22 14:31 被阅读0次

2019年12月15日

urllib库

image.png

一.urllib.request.urlopen

import urllib.request

with urllib.request.urlopen('http://www.sina.com.cn/') as response:
    data = response.read()
    html = data.decode()
    print(html)
image.png

二.GET

结合 urllib.request.urlopen 一起使用

import urllib.request
import urllib.parse

url = 'http://www.51work6.com/service/mynotes/WebService.php'
email = '414875346@qq.com'
params_dict = {'email': email, 'type': 'JSON', 'action': 'query'}
params_str = urllib.parse.urlencode(params_dict)
print(params_str)

url = url + '?' + params_str  # HTTP参数放到URL之后
print(url)

req = urllib.request.Request(url)
with urllib.request.urlopen(req) as response:
    data = response.read()
    json_data = data.decode()
    print(json_data)
image.png

三.POST data=params_bytes

import urllib.parse
import urllib.request

url = 'http://www.51work6.com/service/mynotes/WebService.php'
# 准备HTTP参数
email = '414875346@qq.com'
params_dict = {'email': email, 'type': 'JSON', 'action': 'query'}
params_str = urllib.parse.urlencode(params_dict)
print(params_str)
params_bytes = params_str.encode()  # 字符串转换为字节序列对象

req = urllib.request.Request(url, data=params_bytes)  # 发送POST请求
with urllib.request.urlopen(req) as response:
    data = response.read()
    json_data = data.decode()
    print(json_data)
image.png

四.Downloader例子

import urllib.request

url = 'https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png'

with urllib.request.urlopen(url) as response:
    data = response.read()
    f_name = 'download.png'
    with open(f_name, 'wb') as f:
        f.write(data)
        print('下载文件成功')
image.png

如果您发现本文对你有所帮助,如果您认为其他人也可能受益,请把它分享出去。

相关文章

  • 网络操作操作GET,POST

    2019年12月15日 urllib库 一.urllib.request.urlopen 二.GET 结合 url...

  • Django 学习笔记 - Web基础

    web_basic POST vs GET POST 用于写操作,GET只用于查询POST参数不限长度,GET受限...

  • AJAX

    GET vs. POST GET请求应该只是读操作,POST才应该是对服务端写操作 数据类型Data Type t...

  • 深入了解xUtils

    xUtils分为四大模块: 网络模块:Post , get 请求数据 使用注解联网操作: 快速生成点击事件: ...

  • HttpClient基本操作

    HttpClient 3.1 Get 和 Post 基本操作 RequestEntity StringReques...

  • day46-cookie及session

    1请求方式 请求方式有get和post两种方式;数据查询用get请求数据操作(增、删及改操作)用post请求 1....

  • restful 理解。

    restful是用来描述网络资源,使用GET,POST,PUT,DELETE请求来区分对资源的不同操作。 但是很多...

  • 用原生js封装ajax

    // ajax操作 /*options = { type : "get|post", // 请求方式,默认 "ge...

  • 2018-12-06 RESTful 接口风格

    URL定位资源,用HTTP动词(GET,POST,DELETE,DETC)描述操作。

  • HTTP操作符详解和常用状态码详解

    HTTP与常见操作符 GET和POST的区别 GET1.GET是幂等操作,Get请求不会改变服务器端资源的状态,相...

网友评论

      本文标题:网络操作操作GET,POST

      本文链接:https://www.haomeiwen.com/subject/kdeonctx.html