[置顶] python语言处理get类型请求,调试模式获取数据代码
2015-12-04 22:30
876 查看
#! /usr/bin/env python #coding=utf-8 #version 2.7 import urllib2 import urllib import time import socket bruce_number_max=100 socket.setdefaulttimeout(20) # if __name__ == '__main__': print('Main Thread Run :', __name__) indexValue=0 # httpHandler = urllib2.HTTPHandler(debuglevel=1) httpsHandler = urllib2.HTTPSHandler(debuglevel=1) opener = urllib2.build_opener(httpHandler, httpsHandler) urllib2.install_opener(opener) # while (indexValue<bruce_number_max): url='''http://www.demo.com/index.php?id=NUM''' indexValueString='%u'%indexValue url=url.replace("NUM",indexValueString) url=url.replace(" ","%20") print('[%s]' %(time.strftime('%X'))) print(indexValueString.strip('\n')) print(url) try: req = urllib2.Request(url) req.add_header('User-agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.2) AppleWebKit/525.13 (KHTML, like Gecko) Version/3.1 Safari/525.13') response=urllib2.urlopen(req,timeout=20) html = response.read() print(html) except urllib2.URLError,e: if hasattr(e,"reason"): print ("failed to reach the server") print ("the reason:",e.reason) elif hasattr(e,"code"): print ("the server couldn't fulfill the request") print ("error code:",e.code) print ("return content:",e.read()) else: pass # indexValue=indexValue+1 time.sleep(5) print('\nMain Thread Exit :', __name__)
相关文章推荐
- [置顶] Python语言处理整个文件的base64编码解码源码共享
- 安装pygame for Python3.5
- 趣学Python-教孩子学编程--第十三章
- python基础教程共60课-第3课IDE
- python输出乱码
- python之路——面向对象
- python 调用 C 动态库
- python基础教程共60课-第2课print
- Python中的os模块
- Python 模拟登陆数据系统
- python基础教程共60课-第1课安装
- 《python爬虫学习》:Cookie的使用
- Python读取ini配置文件
- python培训Day6 随笔
- Python -- 单例模式
- Python操作mysql数据库(封装基本的增删改查)
- Python -- 遍历字典时删除元素报异常
- [Python]多线程编程&线程间共享变量&消费者生产者问题的解决
- Python tab键补全
- Python中@的用法(Decorator:装饰器)