一个简单的使用代理访问百度页面内容的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
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
相关文章推荐
- 使用 Suricata 进行入侵监控(一个简单小例子访问百度)
- python,使用代理模式访问淘宝,百度,京东,阿里
- 测试网站页面网速的一个简单Python脚本
- 百度UEditor的简单使用,JSP页面的表单中的内容后台获取和前端配置-结合项目使用
- 使用一个简单的python脚本将一个本地文件以码流的形式,通过UDP协议发送到对端:
- 使用PYTHON3写了一个简单爬虫, 通过公司代理爬取ppt素材
- IronPython for ASP.NET:使用IronPython创建一个简单的Web页面
- python3 使用代理访问
- python 使用代理访问网站
- 利用JS跨域做一个简单的页面访问统计系统
- 请写一个简单的幻灯效果页面。如果不使用JS来完成,可以加分。
- 使用Python实现一个简单的项目监控
- python 一个简单的、迅速生成和执行循环的辅助脚本
- BIEE用户使用代理账号身份访问Analytics页面
- python3使用多代理访问网站
- 使用Vue.js和Element-UI做一个简单的登录页面
- 一个简单的页面,设置背景图平铺、table布局居中、响应式布局及响应式布局中em尺寸单位使用
- python3使用pyqt5制作一个超简单浏览器
- Linux操作系统的简单指令及如何使用vim编写一个程序,然后使用gcc查看【预处理】、【编译】、【汇编】、【链接】各阶段文件的内容。
- 用adb(调试桥)使用python脚本辅助微信小程序跳一跳简单实用教程【新人适用一条龙】附带资源