您的位置:首页 > 编程语言 > Python开发

一个简单的使用代理访问百度页面内容的python脚本

2011-04-13 08:45 876 查看
#!/usr/bin/env python

import urllib2

#代理服务器相关

user='test'

password='123456'

proxyserver='proxy.test.com:'

#要访问的URL为百度首页

url='http://www.baidu.com'

proxy='http://%s:%s@%s' %(user,password,proxyserver)

proxy_handler=urllib2.ProxyHandler({'http':proxy})

#创建opener

opener=urllib2.build_opener(proxy_handler,urllib2.HTTPHandler)

try:

#使用创建的opener访问URL

response=opener.open(url,timeout=3)

#输出页面 status code

print response.code

#输出页面内容

print response.read().decode('gb2312')

#异常处理

except urllib2.HTTPError,e:

print 'The server couldn\'t fulfill the request.'

print 'Error code:',e.code

except urllib2.URLError,e:

print 'We failed to open the URL:%s' %(url)

print 'Reason:',e.reason
本文出自 “梦~从这里起航” 博客,请务必保留此出处http://mayulin.blog.51cto.com/1628315/543559
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐