请看代码:
from html.parser import HTMLParser # pip install html-parser from htmldom import htmldom # pip install htmldom import urllib.request import time H = 'http://www.somewhere.com' f = open('downloaded-novel.html', 'w', encoding='utf-8', newline='\n') f.write('<html><style>*{font:10pt serif}</style>\n') class Index_Parser(HTMLParser): def __init__(m, html): super().__init__(); m.href = None; m.feed(html); def handle_starttag(m, tag, attrs): if tag != 'a': return for name,link in attrs: if name != 'href': continue if not link.startswith('/novel/chapter_'): continue m.href = link; return def handle_data(m, data): if not m.href: return f.write('<h6>' + data + '</h6>\n') url = H + m.href; print(url) dom = htmldom.HtmlDom(url).createDom() for p in dom.find('p'): h = p.html() if h.find('href') == -1: f.writelines((h, '\n')) f.write('\n'); time.sleep(1) # be nice dom = m.href = None Index_Parser(urllib.request.urlopen(H + '/novel/429').read().decode())
浙公网安备 33010602011771号