python中urllib模块用法实例详解

yipeiwu_com5年前Python基础

本文实例讲述了python中urllib模块用法。分享给大家供大家参考。具体分析如下:

一、问题:

近期公司项目的需求是根据客户提供的api,我们定时去获取数据, 之前的方案是用php收集任务存入到redis队列,然后在linux下做一个常驻进程跑某一个php文件, 该php文件就一个无限循环,判断redis队列,有就执行,没有就break.

二、解决方法:

最近刚好学了一下python, python的urllib模块或许比php的curl更快,而且简单. 贴一下代码

复制代码 代码如下:
#_*_ coding:utf-8 _*_
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
import os
import json
from urllib import urlopen
doc = urlopen("http://xxxx?webid=1&tid=901&cateid=101").read()
doc = json.loads(doc)
print doc
print doc.keys()
print doc["msg"]
print doc['data']
print doc['ret']

发现第一次访问所需要的时间为[Finished in 3.0s]
而第二次访问的时间为[Finished in 0.2s]
可见python的urllib模块是加有缓存的
urllib/2用法典型的例子
复制代码 代码如下:
    import urllib2
    import cookielib
    import urllib

    class Hi_login:
        def __init__(self):
            cookie = cookielib.CookieJar()
            self.cookie = urllib2.HTTPCookieProcessor(cookie) ##### 生成cookie ###

        def login(self,user,pwd):
            url='http://passport.baidu.com/?login'
            postdata=urllib.urlencode({
          'mem_pass':'on',
       
          'password':pwd
           'Submit':'',
          'tpl':'sp',
          'tp_reg':'sp',
          'u' :'http://hi.baidu.com',
          'username':user})
            ### proxy_support = urllib2.ProxyHandler({"http":"http://ahad-haam:3128"}) 然后加入opener方法里####
            opener = urllib2.build_opener(self.cookie) ### 使用cookie ###
            headers = { ####### dict结构,可以加入x-forward-for甚至refer等 #######
           'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}
            urllib2.install_opener(opener)
            request = urllib2.Request(url,urllib.urlencode(postdata),headers = headers)
            urllib2.urlopen(request)
    if __name__=='__main__':
       pwd='123456'
       user='xiaofu'
       test=Hi_login()
       test.login(user,pwd)


假如访问需要认证的页面比如nagios监控页面等,
复制代码 代码如下:
    import urllib2
    password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
    url = "http://202.1.x.y/nagios"
    password_mgr.add_password(None, url, user='abc',passwd='xxxxxx')
    handler = urllib2.HTTPBasicAuthHandler(password_mgr)
    opener = urllib2.build_opener(handler)
    urllib2.install_opener(opener)
    f=urllib2.urlopen(url)
    print f.code

返回结果200,否则就是401认证错误

希望本文所述对大家的Python程序设计有所帮助。

相关文章

总结python实现父类调用两种方法的不同

总结python实现父类调用两种方法的不同

python中有两种方法可以调用父类的方法: super(Child, self).method(args)  Parent.method(self, args) 我用其中的一...

WxPython实现无边框界面

wxPython是Python语言的一套优秀的GUI图形库。允许Python程序员很方便的创建完整的、功能键全的GUI用户界面。 wxPython是作为优秀的跨平台GUI库wxWidge...

python 计算两个日期相差多少个月实例代码

python 计算两个日期相差多少个月实例代码

近期,由于业务需要计算两个日期之前相差多少个月。我在网上找了很久,结果发现万能的python,居然没有一个模块计算两个日期的月数,像Java、C#之类的高级语言,都会有(date1-da...

学习python类方法与对象方法

学习python类方法与对象方法

本文实例针对python的类方法与对象方法进行学习研究,具体内容如下 class Test_Demo: TEST = 'test_value' def __init__(s...

python numpy之np.random的随机数函数使用介绍

np.random的随机数函数(1) 函数 说明 rand(d0,d1,..,dn)...