urllib urllib2 自己用

本文对比了Python中URLLib与URLLib2模块的主要区别,URLLib2可以接受Request对象来设置URL请求的headers,而URLLib只能接收URL。此外,URLLib提供了urlencode方法用于生成GET查询字符串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

URllib2 和 URLLib 区别的场合

    urllib2 can accept a Request object to set the headers for a URL request

        urllib2可以接受一个Request对象,并以此可以来设置一个URL的headers

    urllib accepts only a URL

        urllib只接收一个URL

    urllib provides the urlencode method which is used for the generation of GET query strings

        urllib 提供 urlencode方法 它被用作 GET 查询字符串的 生成

        urllib2模块定义的函数和类用来获取URL(主要是HTTP的)

操作打开网页的数据

urllib2.urlopen(url[, data[, timeout[, cafile[, capath[, cadefault[, context]]]]])

data : 默认get 参数,当有数据时,打开post请求 The urllib.urlencode() function takes a mapping or sequence of 2-tuples and returns a string in this format

timeout: 代表 阻塞操作以秒为单位

The optional timeout parameter specifies a timeout in seconds for blocking operations like the connection attemp   

 运用场合:HTTP, HTTPS and FTP connections.

urllib.urlencode(query[doseq]) 

Convert a mapping object or a sequence of two-element tuples to a “percent-encoded” string

功能:转化成一个映射对象,或则由两个元素组成的元组 转化成“百分号编码”字符串

将URL中的键值对以连接符&划分

This is useful to pass a dictionary of form fields to a POST request


转载于:https://my.oschina.net/pythonnnn/blog/660061

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值