发布时间:2019-07-23 09:45:28编辑:auto阅读(1717)
下面是一段用来练手的 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库的使用
48866
47934
38707
35858
30284
27039
26071
20903
20698
19067
502°
594°
604°
609°
587°
572°
637°
710°
828°
928°