71.生成器 http://bbs.fishc.com/forum.php?mod=viewthread&tid=56023&highlight=yield
>>> def myGen():
print('生成器被执行')
yield 1
yield 2
>>> myG=myGen()
>>> next(myG)
生成器被执行
1
>>> next(myG)
2
>>> next(myG)
Traceback (most recent call last):
File "", line 1, in
next(myG)
StopIteration
>>> for i in myGen():
print(i)
生成器被执行
1
2
#用生成器解决斐波那契数列
>>> def Fibs():
a=0
b=1
while True:
a,b=b,a+b
yield a
>>> for each in Fibs():
if each>100:
break
print(each,end=' ')
1 1 2 3 5 8 13 21 34 55 89
>>>
>>>
>>> a=[i for i in range(100) if not(i%2) and i%3]
>>> a
[2, 4, 8, 10, 14, 16, 20, 22, 26, 28, 32, 34, 38, 40, 44, 46, 50, 52, 56, 58, 62, 64, 68, 70, 74, 76, 80, 82, 86, 88, 92, 94, 98]
>>>
>>> b={i:i%2==0 for i in range(10)}
>>> b
{0: True, 1: False, 2: True, 3: False, 4: True, 5: False, 6: True, 7: False, 8: True, 9: False}
>>>
>>> c={i for i in [1,1,2,3,4,5,5,6,7,8,3,2,1]}
>>> c
{1, 2, 3, 4, 5, 6, 7, 8}
>>>
>>> d='i for i in ''jjjj'''
>>>
>>> d
'i for i in jjjj'
72.模块
1)模块就是程序
2)__name__='__main__'
if __name__=='__main__' 代表本模板内部进行
包:
创建一个文件夹,文件夹的名字就是包的名字
再文件夹中创建一个__init__.py的文件,可以为空
将相关模块文件放入文件夹中
导入包:import 包名.模块名 as 模块别名
3)像个极客一样思考
73.爬虫
import urllib.request
responsed=urllib.request.urlopen('www.baidu.com')
html=responsed.read()
html=html.decode('utf-8')
print(html)
如下实战
=============================== RESTART: Shell ===============================
>>> import urllib.request
>>> response=urllib.request.urlopen('http://wwww.baidu.com')
>>> html=response.read()
>>> html=html.decode('utf-8')
>>> print(html)
例子1 猫站
import urllib.request
response=urllib.request.urlopen('http://placekitten.com/g/500/600')
cat_img=response.read()
url=response.geturl()
print(url)
info=response.info()
print(info)
code=response.getcode()
print(code)
with open('cat_500_600.jpg','wb') as f:
f.write(cat_img)
例子2 自动翻译小程序
import urllib.request
import urllib.parse
import json
content=input('请输入需要翻译的内容')
url='http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'
data={}
data['i']=content
data['from']='AUTO'
data['to']='AUTO'
data['smartresult']='dict'
data['client']='fanyideskweb'
data['salt']='1516196611674'
data['sign']='37505adea655f24b61488d0574dffee6'
data['doctype']='json'
data['version']='2.1'
data['keyfrom']='fanyi.web'
data['action']='FY_BY_CLICKBUTTION'
data['typoResult']='false'
data=urllib.parse.urlencode(data)
response=urllib.request.urlopen(url,data)
html=response.read()
target=json.loads(html)
print('翻译结果:%s'%(target['translateResult'][0][0]['tgt']))
例子3 代理ip发送请求
import urllib.request
url='http://www.whatismyip.com.tw'
proxy_support=urllib.request.ProxyHandler({'http':'138.19.213.172:80'})
opener=urllib.request.build_opener(proxy_support)
urllib.request.install_opener(opener)
response=urllib.request.urlopen(url)
html=response.read().decode('urf-8')
print(html)
例子4 妹子图抓取
import urllib.request
import os
def url_open(url):
req=urllib.request.Request(url)
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; …) Gecko/20100101 Firefox/57.0')
response=urllib.request.urlopen(url)
html=response.read()
return html
def get_page(url):
html=url_open(url).decode('utf-8')
a=html.find('current-comment-page')+23
b=html.find(']',a)
print(html[a:b])
return html[a:b]
def find_imgs(url):
html=url_open(url).decode('utf-8')
img_addrs=[]
a=html.find('img src')
while a!=-1:
b=html.find('jpg',a)
if b!=-1:
img_addrs.append(html[a+9:b+4])
else:
b=a+9
a=html.find('img src',b)
return img_addrs
def save_imgs(folder,img_addrs):
for each in img_addrs:
filename=each.split('/')[-1]
with open(filename,'wb')as f:
img=url_open(each)
f.write(img)
def download_mm(folder='xx',pages=10):
os.mkdir(folder)
os.chdir(folder)
url='http://jiandan.net/ooxx'
page_num=int(get_page(url))
for i in range(pages):
page_num-=i
page_url=url+'/page-'+str(page_num)+'#comments'
img_addrs=find_imgs(page_url)
save_imgs(folder,img_addrs)
if __name__=='__main__':
download_mm()
74 正则表达式
1.第一部分
>>> import re
>>> re.search(r'FishC','I love FishC.com!')
<_sre.SRE_Match object; span=(7, 12), match='FishC'>
>>> 'I love FisjC.com!'.find('FishC')
-1
>>> 'I love FishC.com!'.find('FishC')
7
>>> re.search(r'.','I love FishC.com!')
<_sre.SRE_Match object; span=(0, 1), match='I'>
>>> re.search(r'Fish.','I love FishC.com!')
<_sre.SRE_Match object; span=(7, 12), match='FishC'>
>>> re.search(r'\.','I love FishC.com!')
<_sre.SRE_Match object; span=(12, 13), match='.'>
>>> re,search(r'\d','I love 123 FishC.com!')
Traceback (most recent call last):
File "", line 1, in
re,search(r'\d','I love 123 FishC.com!')
NameError: name 'search' is not defined
>>> re.search(r'\d','I love 123 FishC.com!')
<_sre.SRE_Match object; span=(7, 8), match='1'>
>>> re.search(r'\d\d\d\.\d\d\d\.\d\d\d','192.168.111.123')
<_sre.SRE_Match object; span=(0, 11), match='192.168.111'>
>>> re.search(r'[aeiou]',' love FishC.com!')
<_sre.SRE_Match object; span=(2, 3), match='o'>
>>> re.search(r'[a-z]',' love FishC.com!')
<_sre.SRE_Match object; span=(1, 2), match='l'>
>>> re.search(r'[0-9]',' love FishC.com!')
>>> re.search(r'[aeiou]',' love 123 FishC.com!')
<_sre.SRE_Match object; span=(2, 3), match='o'>
>>> re.search(r'ab{3}c','abbbc')
<_sre.SRE_Match object; span=(0, 5), match='abbbc'>
>>> re.search(r'ab{3,10}c','abbbbbbbc')
<_sre.SRE_Match object; span=(0, 9), match='abbbbbbbc'>
>>> re.search(f'[01]\d\d|2[0-4]\d|25[0-5]','188')
SyntaxError: invalid syntax
>>> re.search(r'[01]\d\d|2[0-4]\d|25[0-5]','188')
<_sre.SRE_Match object; span=(0, 3), match='188'>
>>> re.search(r'(([01]{0,1}\d{0,1}\d|2[0-4]\d|25[0-5])\.){3}([01]{0,1}\d{0,1}\d|2[0-4]\d|25[0-5])','192.168.1.1')
<_sre.SRE_Match object; span=(0, 11), match='192.168.1.1'>
>>>
2.第二部分
>>> re.search(r'fish(C|D)','FishhC')
>>> re.search(r'Fish(C|D)','FishhC')
>>> re.search(r'Fish(C|D)','FishC')
<_sre.SRE_Match object; span=(0, 5), match='FishC'>
>>> re.search(r'Fish(C|D)','FishD')
<_sre.SRE_Match object; span=(0, 5), match='FishD'>
>>> re.search(r'^FishC','I love FishC.com!')
>>> re.search(r'^FishC','FishC.com!')
<_sre.SRE_Match object; span=(0, 5), match='FishC'>
>>> re.search(r'FishC$','FishC.com')
>>> re.search(r'FishC$','love FishC)
SyntaxError: EOL while scanning string literal
>>> re.search(r'FishC$','love FishC')
<_sre.SRE_Match object; span=(5, 10), match='FishC'>
>>> re.search(r'(FishC)\1','FishC.com')
>>> re.search(r'(FishC)\1','FishCFishC)
SyntaxError: EOL while scanning string literal
>>> re.search(r'(FishC)\1','FishCFishC')
<_sre.SRE_Match object; span=(0, 10), match='FishCFishC'>
>>> re.search(r'[.]','FishC.com')
<_sre.SRE_Match object; span=(5, 6), match='.'>
>>> re.findall(r'[a-z]','FishC.com')
['i', 's', 'h', 'c', 'o', 'm']
>>> re.search(r'[^a-z]','FishC.com\n')
<_sre.SRE_Match object; span=(0, 1), match='F'>
>>> re.search(r'[a-z^]','FishC.com\n')
<_sre.SRE_Match object; span=(1, 2), match='i'>
>>> re.findall(r'[^a-z]','FishC.com\n')
['F', 'C', '.', '\n']
>>> re.search(r'FishC{3}','FishCCC')
<_sre.SRE_Match object; span=(0, 7), match='FishCCC'>
>>> re.search(r'(FishC){3}','FishCCC')
>>> re.search(r'(FishC){3}','FishCFishCFishC')
<_sre.SRE_Match object; span=(0, 15), match='FishCFishCFishC'>
>>> re.search(r'(FishC){13}','FishCFishCFishC')
>>> re.search(r'(FishC){1-3}','FishCFishCFishC')
>>> re.search(r'(FishC){1,3}','FishCFishCFishC')
<_sre.SRE_Match object; span=(0, 15), match='FishCFishCFishC'>
>>> s='I love FishC.com'
>>> re.search(r'<.+>',s)
<_sre.SRE_Match object; span=(0, 44), match='I love FishC.com'>
>>> re.search(r'<.+?>',s)
<_sre.SRE_Match object; span=(0, 6), match=''>