#获取文件目录下文件夹和子文件夹的文件名
import os
def walk_dir(dirname):
filelist=[]
for root,dirs,filesin os.walk(dirname):
forname in files:
filelist.append(os.path.join(root,name))
printfilelist
returnfilelist
dirname =r"C:\Users\intern\Desktop\aa"
walk_dir(dirname)
#遍历列表的嵌套
a = [["a","b"],["a",["b",["c"]]],["a",["b"]]]
def walk(b):
for iin b:
print type(i)
if type(i)is list:
walk(i)
else:
printi
walk(a)
#字符串列表转换成指定的字典,
a = ["jack=hello","rose=happy","mike=83"]
b = {}
fori in a:
path = 0
point = -1
c = d =""
for jin i:
if(j !="=")and(point == path-1):
c += j
point = path
else:
if(j!="="):
d+=j
path+=1
b.setdefault(c,d)
printb
全排列数字
def print_permutation(n,A,cur):
if cur==n:
for i in xrange(0,n):
print A[i],
print ''
else:
for i in xrange(1,n+1):
ok=True
for j in xrange(0,cur):
if A[j]==i:
ok=False
break
if ok:
A[cur]=i
print_permutation(n,A,cur+1)
A=[0,0,0,0,0,0,0,0]
cur=0
n=5
print_permutation(n,A,cur)
或者使用
import itertools
horses = [1,2,3,4,5]
races = itertools.permutations(horses)
print (races) 此处需要注意的是返回的是内存地址
print (list(itertools.permutations(horses)))
多进程模板
import multiprocessing,requests
def get(url):
r = requests.get(url)
print r.elapsed.microseconds
def process_main(url):
for i in range(num_cpus):
t = multiprocessing.Process(target=get,args=[url,])
t.start()
processes.append(t)
if __name__=="__main__":
url = "htt...."
processes=[]
process_main(url)
for p in processes:
p.join()
代理爬虫,bs
from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
import requests
session = requests.Session()
headers = {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit 537.36 (KHTML, like Gecko) Chrome',
'Accept':'text/html, application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'}
i = 1
fp = open('result.txt','w')
while True:
try:
url = "http://bulo.hujiang.com/group/tensei/topic?page=%d"%i
req = session.get(url, headers=headers).text
bsObj = BeautifulSoup(req, 'html.parser')
for link in bsObj.find("div", {"id": "sideleft"}).findAll("a", href=re.compile("/group/topic/\d+/")):
if 'href' in link.attrs:
a = link.attrs['href']
fp.write(a)
i += 1
except Exception as e :
print(e)
fp.close()
break