#字符串到数字 可以进行各种进制转换
#print(int('11111',8))
#需求 对字符串中的单词翻转 保留空格
#第一种方法 最笨
clstr='I love you!'
bam=clstr.split(' ')
rebam=bam[2]+' '+bam[1]+' '+bam[0]
#print(rebam)
#第二种方法
def reverse_word(s):
s1=s.split(' ')
i=0
j=len(s1)-1
while i<j:
"""
temp=s1[i]
s1[i]=s1[j]
s1[j]=temp
"""
#直接交换位置
s1[i],s1[j]=s1[j],s1[i]
i+=1
j-=1
return ' '.join(s1)
#字典 dict
def func(p):
print('x=',p['x'])
print('y=',p['y'])
#**表示是一个字典 key/value类型
def dcfunc(name,**dicaddr):
print(name)
print(type(dicaddr))
print(dicaddr)
#可变参数 *numberis 是一个可变的参数 tuple
def tufunc(name,*numbers):
print(type(numbers))
print(numbers)
#递归
def my_sum(i):
if i < 0:
raise ValueError
elif i <= 1:
return i
else:
return i + my_sum(i-1)
#斐波那数列
def feibona(count):
a=1
b=1
while a<count:
print(a)
a,b=b,a+b
def fibo(num):
if num==1 or num==2:
return 1
else:
return fibo(num-2)+fibo(num-1)
#print(fibo(15))
def gerFiboList(num):
fiboList=[]
for i in range(1,num+1):
fiboList.append(fibo(i))
return fiboList
#print(gerFiboList(15))
def hano(n,a,b,c):
print(n)
if n==1:
print(a,"->",c)
else:
#将n-1个盘子从x->y
hano(n-1,a,c,b)
#将剩余的最后一个盘子从x->z
hano(1,a,b,c)
#将剩余的n-1个盘子从y->z
hano(n-1,b,a,c)
if __name__=='__main__':
s='I love you! hello world'
#print(reverse_word(s))
#func({'x':100,'y':200})
#tuple 元组
#tufunc('jack',2,34,45,32,3)
#dcfunc('batman',china='北京',eluoshi='克林姆宫',taiguo='曼谷')
#print(my_sum(1))
#print(my_sum(2))
#print(my_sum(5))
#feibona(50)
#gerFiboList(50)
print('---------------------')
#print(gerFiboList(20))
#print(fibo(20))
hano(4,'A','B','C')
import urllib.parse
import urllib.request
import re
import os
def handle_request(url,page):
url = url+str(page)+'/'
headers={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36',
}
request=urllib.request.Request(url=url,headers=headers)
return request
def download_image(content):
pattern=re.compile(r'<div class="thumb">.*?<img src="(.*?)".*?>.*?</div>',re.S)
lt=pattern.findall(content)
#print(lt)
for link in lt:
link='https:'+link
dirname='qiutu'
if not os.path.exists('qiutu'):
os.mkdir(dirname)
filename=link.split('/')[-]
filepath=dirname+'/'+filename
print('%s图片开始下载......'%filename)
urllib.request.urlretrieve(link,filepath)
print('%s图片结束下载......'%filename)
def main():
url='http://www.qiushibaike.com/pic/page/'
start_page=int(input("请输入起始页码:"))
end_page=int(input("请输入结束页码:"))
for page in range(start_page,end_page+):
print('第%s页开始下载......'%page)
print('*'*)
request=handle_request(url,page)
content=urllib.request.urlopen(request).read().decode()
#print(content)
#解析内容,提取所有的图片链接,下载图片
download_image(content)
print('第%s页结束下载......'%page)
print('*'*)
if __name__=='__main__':
main()