Home  >  Article  >  Backend Development  >  python中urllib模块用法实例详解

python中urllib模块用法实例详解

WBOY
WBOYOriginal
2016-06-10 15:18:541324browse

本文实例讲述了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程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn