python下载了红楼梦全文经典读着不错吗「python下载了红楼梦全文经典读着不错」

时间:2023-06-20 16:20:02阅读:16
想看《红楼梦》,就在网上搜了一下,不想在线看,就给爬下来了。然后常规,下面是代码:import requestsfrom lxml import etreedef save(url): try: r = requests.get(url) novel = e…

想看《红楼梦》,就在网上搜了一下,不想在线看,就给爬下来了。

然后常规,下面是代码:

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 = 'http://st.kanxshuo.com/book-15-' for page in range(1, 222): url = base_url str(page) '.html' save(url)if __name__ == '__main__': main()

评论

  • 评论加载中...