想看《红楼梦》,就在网上搜了一下,不想在线看,就给爬下来了。
然后常规,下面是代码:
import requestsfrom lxml import etreedef save(url): try: r = requests.get(url) novel = etree.HTML(r.text).xpath('//*[@id="fontzoom"]/text()') with open('红楼梦.txt', 'a', encoding='utf-8') as f: for pag in novel: f.write(pag+'\n') print('保存成功') except: passdef main(): base_url = '()