python中urllib模块用法实例详解
作者:shichen2014 时间:2022-02-05 13:23:33
本文实例讲述了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程序设计有所帮助。
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python XlsxWriter模块创建aexcel表格的实例讲解
![](https://img.aspxhome.com/file/2023/3/65093_0s.jpg)
python中用ggplot绘制画图实例讲解
![](https://img.aspxhome.com/file/2023/1/77041_0s.jpg)
间歇向上无缝翻滚代码
Python3利用Dlib19.7实现摄像头人脸识别的方法
![](https://img.aspxhome.com/file/2023/1/76031_0s.gif)
Go流程控制代码详解
使用 createProcessingInstruction 方法不能输出 encoding 的解决方法
Go语言区别于其他语言的特性
tensorflow基于CNN实战mnist手写识别(小白必看)
![](https://img.aspxhome.com/file/2023/2/99432_0s.png)
python列表推导式入门学习解析
python复制文件到指定目录的实例
firefox通过XUL实现text-overflow:ellipsis的效果
深入浅析python 协程与go协程的区别
js 混淆加密工具代码
Python3中常用的处理时间和实现定时任务的方法的介绍
JavaScript中Object基础内部方法图
![](https://img.aspxhome.com/file/2023/1/56071_0s.jpg)
Python利用plotly绘制正二十面体详解
![](https://img.aspxhome.com/file/2023/7/97707_0s.png)
Web标准学习:CSS样式书写风格
![](https://img.aspxhome.com/file/UploadPic/20083/25/200832593821947s.gif)