Python网络请求模块urllib与requests使用介绍

作者:Python热爱者 时间:2021-07-21 12:23:38 

Python 网络请求模块 urllib 、requests

Python 给人的印象是抓取网页非常方便,提供这种生产力的,主要依靠的就是 urllib、requests这两个模块。

urlib 介绍

  • urllib.request 提供了一个 urlopen 函数,来实现获取页面。支持不同的协议、基本验证、cookie、代理等特性。

  • urllib 有两个版本 urllib 以及 urllib2。

  • urllib2 能够接受 Request 对象,urllib 则只能接受 url。

  • urllib 提供了 urlencode 函数来对GET请求的参数进行转码,urllib2 没有对应函数。

  • urllib 抛出了 一个 URLError 和一个 HTTPError 来处理客户端和服务端的异常情况。

Requests 介绍

Requests 是一个简单易用的,用Python编写的HTTP库。这个库让我们能够用简单的参数就完成HTTP请求,而不必像 urllib 一样自己指定参数。同时能够自动将响应转码为Unicode,而且具有丰富的错误处理功能。

  • International Domains and URLs

  • Keep-Alive & Connection Pooling

  • Sessions with Cookie Persistence

  • Browser-style SSL Verification

  • Basic/Digest Authentication

  • Elegant Key/Value Cookies

  • Automatic Decompression

  • Unicode Response Bodies

  • Multipart File Uploads

  • Connection Timeouts

  • .netrc support

  • List item

  • Python 2.6—3.4

  • Thread-safe

以下为一些示例代码,本文环境为 Python 3.6

无需参数直接请求单个页面

import urllib
from urllib.request import request
from urllib.urlopen import urlopen
# import urllib2
import requests
# 使用 urllib 方式获取
response = urllib.request.urlopen('http://www.baidu.com')
# read() 读取的是服务器的原始返回数据 decode() 后会进行转码
print(response.read().decode())
# 使用 requests 方式获取
# request 模块相比
resp = requests.get('http://www.baidu.com')
print(resp)
print(resp.text)

HTTP 是基于请求和响应的工作模式,urllib.request 提供了一个 Request 对象来代表请求,因此上面的代码也可以这么写

req = urllib.request.Request('http://www.baidu.com')
with urllib.request.urlopen(req) as response:
print(response.read())

Request对象可以增加header信息

req = urllib.request.Request('http://www.baidu.com')
req.add_header('User-Agent', 'Mozilla/6.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/8.0 Mobile/10A5376e Safari/8536.25')
with urllib.request.urlopen(req) as response:
print(response.read())

或者直接将 header 传入 Request 构建函数。

带参数的 GET 请求

带有参数的请求和上面的例子本质一样,可以事先拼出URL请求字符串,然后再进行请求。

本例使用了 腾讯 的股票API,可以传入不同的股票代码以及日期,查询对应股票在对应时间的价格、交易信息。

# 使用带参数的接口访问
tencent_api = "http://qt.gtimg.cn/q=sh601939"
response = urllib.request.urlopen(tencent_api)
# read() 读取的是服务器的原始返回数据 decode() 后会进行转码
print(response.read())
resp = requests.get(tencent_api)
print(resp)
print(resp.text)

发送 POST 请求

urllib 没有单独区分 GET 和 POST 请求的函数,只是通过 Request 对象是否有 data 参数传入来判断。

import urllib.parse
import urllib.request
url = 'http://www.someserver.com/cgi-bin/register.cgi'
values = {'name' : 'Michael Foord',
         'location' : 'Northampton',
         'language' : 'Python' }
data = urllib.parse.urlencode(values)
data = data.encode('ascii') # data should be bytes req = urllib.request.Request(url, data)
with urllib.request.urlopen(req) as response:
  the_page = response.read()

来源:https://blog.csdn.net/qdPython/article/details/127184235

标签:Python,urllib,requests,模块
0
投稿

猜你喜欢

  • python中torch.nn.identity()方法详解

    2021-05-21 16:51:01
  • 编写Smarty插件在模板中直接加载数据的详细介绍

    2023-11-15 09:14:47
  • 后工业时代的后规范思考2——如何应用

    2009-06-05 12:35:00
  • Python如何根据时间序列数据作图

    2022-06-30 01:41:01
  • python文件目录操作之os模块

    2023-01-10 14:22:59
  • 什么是 XML Web Service

    2008-09-05 17:21:00
  • python识别围棋定位棋盘位置

    2023-01-09 01:49:34
  • python DataFrame中stack()方法、unstack()方法和pivot()方法浅析

    2023-12-13 17:38:55
  • 微信小程序实现图片上传功能实例(前端+PHP后端)

    2023-11-05 14:19:27
  • python私有属性和方法实例分析

    2023-11-21 06:16:13
  • PyTorch模型转换为ONNX格式实现过程详解

    2022-03-18 00:54:18
  • Python基础教程之异常详解

    2022-02-08 05:48:49
  • python 切片和range()用法说明

    2021-12-12 07:40:52
  • Python实现多线程的两种方式分析

    2022-01-06 22:36:10
  • 无组件上传图片到数据库中,asp解决方案

    2007-08-03 13:22:00
  • 微软开源最强Python自动化神器Playwright(不用写一行代码)

    2024-01-02 00:38:00
  • echo(),print(),print_r()之间的区别?

    2023-11-15 08:52:42
  • Python使用GitPython操作Git版本库的方法

    2021-04-28 07:22:16
  • Python第三方库的几种安装方式(小结)

    2021-11-01 19:34:09
  • PHP中include和require的使用详解

    2023-10-22 03:57:03
  • asp之家 网络编程 m.aspxhome.com