装饰器的主要作用是,在不修改其他函数的前提下,为被封装函数添加代码,装饰器语法优美,big很高
python装饰器一个使用的例子,主要功能是爬取网页,然后计算爬了多长时间 Continue reading Python装饰器使用例子 网页爬取测速
Tag: python
python模拟Linux shell登陆
本程序主要是实现对python装饰器的进一步利用, 执行的过程如下:
- 用户输入用户名密码
open()
读取用户文件,判断用户名密码,判断是否锁定- 如果用户登入错误次数等于2则代表已经被锁定,则执行下一次输入
- 如果用户没有被锁定,但用户名密码错误,则提示用户名密码错误,并在文件内把登入错误计数加1,进入下一次输入
- 如果经过上面的判断,则直接进入
login_index
函数(shell函数)
python对文件的增删改查
python对配置文件操作,对文件的增\删\改\查,分享下思路
Continue reading python对文件的增删改查
Python遍历嵌套List方法
脑子不太好想记下点东西,主要实现的关键在于last_menu
和count1
,last_menu
存储进入下一个list之前li
的值,count1
存储进入下一个liist之前,li
的下一个位置的索引. Continue reading Python遍历嵌套List方法
Python无限嵌套多级菜单
无限嵌套菜单很费脑,分享一下思路
Continue reading Python无限嵌套多级菜单
pip下载慢?不妨试试修改镜像
pip 为 Python 的包管理提供了极大的方便,省去了手动搜索、下载、编译的麻烦。 Continue reading pip下载慢?不妨试试修改镜像
用Python来查快递
本项目利用python与快递100接口进行快递查询,如违反了相关规定请在文章下方留言,立即删除
# -*- coding:utf-8 -*-
__auther__ = 'slll.info'
__date__ = '2016-7'
#module import
import requests,json
#function
def spider(type,id):
url = 'https://www.kuaidi100.com/query?type=%s&postid=%s' %(type,id)
data = requests.get(url)
json = data.json()
if json['status'] == "200":
data_json = json['data']
print("//////////////快递详细信息//////////////")
for x in data_json:
print("%s : %s" %(x['time'],x['context']))
else:
print("错误的快递单号!")
def express_type_get():
express_type = ('shunfeng','yunda','shentong','yuantong','zhongtong','ems','tiantian','huitongkuaidi','quanfengkuaidi','youzhengguonei')
print('////////////////快递公司////////////////\n1.顺丰 2.韵达 3.申通 4.圆通 5.中通\n6.EMS 7.天天 8.汇通 9.全峰 10.邮政\n////////////////////////////////////////')
while True:
express = int(input('请选择快递公司(数字):'))
if express:
if express <= 10 and express >= 1:
break
else:
print("错误的选择!")
else:
print("不能为空!")
return express_type[express-1]
def express_id_get():
while True:
express_id = input('请输入快递单号:')
if express_id:
break
else:
print("快递单号不能为空!")
return express_id
#Mainprogram
kd = express_type_get()
kd_id = express_id_get()
spider(kd,kd_id)
</pre>
Windows网卡叠加
以前经常在网上找网卡叠加的小软件,找过很多个,有的用不来有的没效果,偶尔找到一个能用的批处理,于是根据这个脚本自己用python写了一个修改路由表的方案,这样一来下次就不用在网上找来找去了,简单实用(水平有限,还请在座各位多多指教)。 废话不多说直接贴代码,送给需要的人
#coding:utf-8
#调用库
import sys,os,re
#函数
def pro_continue():
input("按Enter键退出")
def nic_count(x):
if x<2:
print("网络叠加需要两块或两块以上网卡")
exit()
elif x>4:
print("该程序最多支持叠加四块网卡")
exit()
def add_routetables2(i,g):
net_1=[1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59,61,63,65,67,69,71,73,75,77,79,81,83,85,87,89,91,93,95,97,99,101,103,105,107,109,111,113,115,117,119,121,123,125,129,131,133,135,137,139,141,143,145,147,149,151,153,155,157,159,161,163,165,167,171,173,175,177,179,181,183,185,187,189,191,193,195,197,199,201,203,205,207,209,211,213,215,217,219,221,223]
net_2=[2,4,6,8,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,92,94,96,98,100,102,104,106,108,110,112,114,116,118,120,122,124,126,128,130,132,134,136,138,140,142,144,146,148,150,152,154,156,158,160,162,164,166,168,170,174,176,178,180,182,184,186,188,190,194,196,198,200,202,204,206,208,210,212,214,216,218,220,222]
print("开始负载均衡")
os.system("route delete 0.0.0.0")
os.system("route add 0.0.0.0 mask 0.0.0.0 " + str(g[0]) + " metric 30 if " + str(i[0]))
a=0
for x in net_1:
os.system ("route add " + str(x) + ".0.0.0 mask 255.0.0.0 "+ str(g[0]) +" metric 25 if " + str(i[0]))
for x in net_2:
os.system ("route add " + str(x) + ".0.0.0 mask 255.0.0.0 "+ str(g[1]) +" metric 25 if " + str(i[1]))
print("双网卡叠加成功")
def add_routetables3(i,g):
net_1=[1,4,7,13,16,19,22,25,28,31,34,37,40,43,46,49,52,55,58,61,64,67,70,73,76,79,82,85,88,91,94,97,100,103,106,109,112,115,118,121,124,130,133,136,139,142,145,148,151,154,157,160,163,166,175,178,181,184,187,190,193,196,199,202,205,208,211,214,217,220,223]
net_2=[2,5,8,11,14,17,20,23,26,29,32,35,38,41,44,47,50,53,56,59,62,65,68,71,74,77,80,83,86,89,92,95,98,101,104,107,110,113,116,119,122,125,128,131,134,137,140,143,146,149,152,155,158,161,164,167,170,173,176,179,182,185,188,191,194,197,200,203,206,209,212,215,218,221]
net_3=[3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87,90,93,96,99,102,105,108,111,114,117,120,123,126,129,132,135,138,141,144,147,150,153,156,159,162,165,168,171,174,177,180,183,186,189,195,198,201,204,207,210,213,216,219,222]
print("开始负载均衡")
os.system("route delete 0.0.0.0")
os.system("route add 0.0.0.0 mask 0.0.0.0 " + str(g[0]) + " metric 30 if " + str(i[0]))
a=0
for x in net_1:
os.system ("route add " + str(x) + ".0.0.0 mask 255.0.0.0 "+ str(g[0]) +" metric 25 if " + str(i[0]))
for x in net_2:
os.system ("route add " + str(x) + ".0.0.0 mask 255.0.0.0 "+ str(g[1]) +" metric 25 if " + str(i[1]))
for x in net_3:
os.system ("route add " + str(x) + ".0.0.0 mask 255.0.0.0 "+ str(g[2]) +" metric 25 if " + str(i[2]))
print("三网卡叠加成功")
def add_routetables4(i,g):
net_1=[1,5,9,13,17,21,25,29,33,37,41,45,49,53,57,61,65,69,73,77,81,85,89,93,97,101,105,109,113,117,121,125,129,133,137,141,145,149,153,157,161,165,173,177,181,185,189,193,197,201,205,209,213,217,221]
net_2=[2,6,14,18,22,26,30,34,38,42,46,50,54,58,62,66,70,74,78,82,86,90,94,98,102,106,110,114,118,122,126,130,134,138,142,146,150,154,158,162,166,170,174,178,182,186,190,194,198,202,206,210,214,218,222]
net_3=[3,7,11,15,19,23,27,31,35,39,43,47,51,55,59,63,67,71,75,79,83,87,91,95,99,103,107,111,115,119,123,131,135,139,143,147,151,155,159,163,167,171,175,179,183,187,191,195,199,203,207,211,215,219,223]
net_4=[4,8,12,16,20,24,28,32,36,40,44,48,52,56,60,64,68,72,76,80,84,88,92,96,100,104,108,112,116,120,124,128,132,136,140,144,148,152,156,160,164,168,176,180,184,188,196,200,204,208,212,216,220]
print("开始负载均衡")
os.system("route delete 0.0.0.0")
os.system("route add 0.0.0.0 mask 0.0.0.0 " + str(g[0]) + " metric 30 if " + str(i[0]))
a=0
for x in net_1:
os.system ("route add " + str(x) + ".0.0.0 mask 255.0.0.0 "+ str(g[0]) +" metric 25 if " + str(i[0]))
for x in net_2:
os.system ("route add " + str(x) + ".0.0.0 mask 255.0.0.0 "+ str(g[1]) +" metric 25 if " + str(i[1]))
for x in net_3:
os.system ("route add " + str(x) + ".0.0.0 mask 255.0.0.0 "+ str(g[2]) +" metric 25 if " + str(i[2]))
for x in net_4:
os.system ("route add " + str(x) + ".0.0.0 mask 255.0.0.0 "+ str(g[3]) +" metric 25 if " + str(i[3]))
print("四网卡叠加成功")
def check_ip(ip_str):
pattern = r"\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
if re.match(pattern, ip_str):
return True
else:
return False
#主程序
os.system("title 网卡叠加-www.slll.info&&color 18")
net_count=int(input("请输入网卡数量(MAX:4,Min:2): "))
nic_count(net_count)
arr_1=[]
arr_2=[]
for x in range(1,net_count+1):
temp=input("请输入第"+str(x)+"块需要叠加的网卡索引号 (cmd下面利用该命令查看:route print | find \"...\"[第一列即索引号]): ")
arr_1.append(temp)
temp=input("请输入网卡(" +str(x)+") 的网关: ")
while True:
if check_ip(temp):
arr_2.append(temp)
break
else:
temp=input("输入错误,请重新输入网卡(" +str(x)+") 的网关: ")
if net_count==2:
add_routetables2(arr_1,arr_2)
elif net_count==3:
add_routetables3(arr_1,arr_2)
elif net_count==4:
add_routetables4(arr_1,arr_2)
pro_continue()
Pyquery获取网页数据的一个小例子
最近找到一个免费的代理,他们把连接参数放在网页上,两天一改,每次开电脑都要检查一下数据是否有更新,然后再改写文件,步骤比较麻烦,想到前几天看过一本python的书,于是用python写个一个抓取网页数据的小程序,万万没想到几条代码就能实现,终于知道为什么网上流传这“python大法好”、“人生苦短,我用python”此类的话了。
写这个程序之前啊,首先要安装python的pyquery库
yum install python-pyquery
这里以获取ZOL手机周榜为例子,附上的代码
#coding=utf-8
import pyquery,sys #调用模块
reload(sys) #虽然不知到为什么要重载SYS模块,但不这样不行
sys.setdefaultencoding('utf-8') #设置默认字符编码
web = pyquery.PyQuery(url='http://detail.zol.com.cn/cell_phone_index/subcate57_0_list_1_0_8_1_0_1.html') #抓取页面
web_content = web.find('body') #获取页面标签的内容
web_content_cov = web_content.text() #页面转文本
phone_list_num = web_content_cov.find('本周') #获取本周第一次出现的位置
print web_content_cov[phone_list_num:phone_list_num+266] #截取字符串
写到文件的话
writefile = open('/home/XXXXX/Documents/phone.txt','w') #写入模式打开文本文件
writefile.write(web_content_cov[phone_list_num:phone_list_num+266]) #写入字符串,简单的不能再简单