发布时间:2019-07-23 09:45:28编辑:auto阅读(2506)
下面是一段用来练手的 python 捉取html 内容的代码:
import urllib2
import os
import re
#打开并保存hmtl
def save_html(urlname):
main_url=r'https://docs.python.org/2/library/'
main_dir=r'E:BOOKpythonpython_library'
url=main_url+urlname+'.html'
file_name=main_dir+'\' +urlname+'.html'
try:
req=urllib2.urlopen(url)
urlfile=open(file_name,'w')
urlfile.write(req.read())
except:
print url
finally:
urlfile.close()
#保存主页
save_html('index')
#正则表达式查找链接并保存对应文件
req=urllib2.urlopen(r'https://docs.python.org/2/library/index.html')
p=re.compile(r'''<li class="toctree-.+?"><a class="reference internal" href="(.+?).html">.+?</a></li>''')
matchs=p.findall(req.read())
for row in matchs:
save_html(row)捉完后,使用了 [ calibre - E-book management ] 把html 转换成mobi 格式的文件。
mobi 下载链接:
http://f.dataguru.cn/forum.php?mod=attachment&aid=MTQ5OTQzfDc1Y2MyMDk5fDE0MDgxNzEzNTB8NDQxMTd8MzM3NjMy
上一篇: Python if 和 for 的多种写
下一篇: python uuid库的使用
54841
41623
36206
31655
26557
26257
24783
20375
16325
15749
2838°
2594°
2689°
2830°
2778°
3002°
2918°
2754°
2755°
2730°