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

[置顶] 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__)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: