标签:style blog http color os 使用 ar for sp
在了解python基础的语法基础上,就可以自由的去组合出自己需要各类高级功能。
import sys,urllib,urllib2 url=input("please input the url:") url = "http://mail.126.com" #发起请求 req = urllib2.Request(url) fd = urllib2.urlopen(req) #输出结果 #print fd.read() print ("URL Retrieved:",fd.geturl()) info = fd.info() for key, value in info.items(): print "%s = %s" % (key,value) while True: data = fd.read(1024) if not len(data): break #print data
#构造get参数 #zipcode = sys.argv[1] wd = input("search word:") data = urllib.urlencode([(‘wd‘,wd)]) #构造url 拼接请求参数 url= "http://www.baidu.com" url = url + "?" + data print ‘ursing url‘, url #构造request 并请求 req = urllib2.Request(url) fd = urllib2.urlopen(req) #读取相应结果 while True: data = fd.read(1024) if not len(data): break #sys.stdout.write(data) print data
import sys,urllib,urllib2 #构造参数 #zipcode = sys.argv[1] wd = input("search word:") data = urllib.urlencode([(‘wd‘,wd)]) #构造url 不需要将参数拼接到url中 url= "http://www.baidu.com" print ‘ursing url‘, url #构造request 只需要将参数放到urlopen的第二个参数里 req = urllib2.Request(url) fd = urllib2.urlopen(req,data) #读取相应结果 while True: data = fd.read(1024) if not len(data): break #sys.stdout.write(data) print data
import sys,urllib,urllib2,getpass #定义TerminalPwd类扩展HTTPPasswordMgr,允许在需要的时候询问操作员输入密码 class TerminalPwd(urllib2.HTTPPasswordMgr): def find_user_password(self,realm,authuri): retval = urllib2.HTTPPasswordMgr.find_user_password(self,realm,authuri) if retval[0] == None and retval[1] == None: #didn‘t find it in stored values username = input("Login required,please input username:") password = input("please input password:") return(username,password) else: return retval url = "http://home.asiainfo.com/" req = urllib2.Request(url) #需要加载额外的处理时需要使用opener,比如此处需要支持认证处理 #如果需要认证,会自动调用TerminalPwd里面的函数,如果不需要进一步检查和普通的请求一样 opener = urllib2.build_opener(urllib2.HTTPBasicAuthHandler(TerminalPwd())) #请求 fd = opener.open(req) print ("URL Retrieved:",fd.geturl()) info = fd.info() for key, value in info.items(): print "%s = %s" % (key,value)
标签:style blog http color os 使用 ar for sp
原文地址:http://www.cnblogs.com/alwaysthinking/p/4020875.html