urllib 和urllib2之间的区别

来源:互联网 发布:yum install什么意思 编辑:程序博客网 时间:2024/05/15 18:02


作为一个Python菜鸟,之前一直懵懂于urllib和urllib2,以为2是1的升级版。今天看到老外写的一篇《Python: difference between urllib and urllib2》才明白其中的区别。

 

You might be intrigued by the existence of two separate URL modules in Python -urllib and urllib2. Even more intriguing: they are not alternatives for each other. So what is the difference between urllib and urllib2, and do we need them both? 

 

你可能对于Python中两个独立存在的-urllib2和-urllib2感到好奇。更有趣的是:它们并不是可以相互代替的。那么这两个模块间的区别是什么,并且这两个我们都需要吗?

urllib 
and  
urllib2are both Python modules that do URL request related stuff but offer different functionalities. Their two most significant differences are listed below: 

urllib 和urllib2都是接受URL请求的相关模块,但是提供了不同的功能。两个最显著的不同如下:

  • urllib2 can accept a Request object to set the headers for a URL request,urllib accepts only a URL. That means, you cannot masquerade your User Agent string etc.

urllib2可以接受一个Request类的实例来设置URL请求的headers,urllib仅可以接受URL。这意味着,你不可以伪装你的User Agent字符串等。

 

  • urllib provides the urlencode method which is used for the generation of GET query strings, urllib2 doesn't have such a function. This is one of the reasons why urllib is often used along with urllib2.

urllib提供urlencode方法用来GET查询字符串的产生,而urllib2没有。这是为何urllib常和urllib2一起使用的原因。


For other differences between urllib and urllib2 refer to their documentations, the links are given in the References section. 

Tip: if you are planning to do HTTP stuff only, check out httplib2, it is much better than httplib or urllib or urllib2. 

提示:如果你仅做HTTP相关的,看一下httplib2,比其他几个模块好用。

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 江里水流大跑线怎么办 js逻辑好难怎么办 怀孕了好无聊怎么办 做表率我们怎么办心得 晚上左转逆行了怎么办 鼓浪屿船票买不到怎么办 信用卡逾期房贷批不下来怎么办 房贷逾期还款怎么办 网被垄断了怎么办? 百度搜索网页打不开怎么办? 原生家庭很糟糕怎么办 白色油漆变黄怎么办 白油漆家具泛黄怎么办 交通电子眼60分怎么办 被公司罚款了怎么办 公司对员工罚款怎么办 高一军训洗漱怎么办 吃精神病药发胖怎么办 神经病人不吃药怎么办 事故全责不赔钱怎么办 吃精神药物发胖怎么办 郑州出院限号怎么办 林场改革森林消防怎么办 网络课没有看完怎么办 福建省会计证未换怎么办 国税地税登记后怎么办 网上发票领不了怎么办 合肥餐饮怎么办环评 地税财务报表上传错误怎么办? 平安体检卡过期怎么办 到医院挂号后怎么办 一吹风就头痛怎么办 平安口袋e怎么办离职 学员学分卡号怎么办 华医网注册错了怎么办 医师继续教育学分不够怎么办 执业医师学分卡怎么办 护士学分5年没办怎么办 华医网职称录入错误怎么办 在北京没有学历怎么办 继续教育证书丢了怎么办