import requests from lxml import etree import time next_url = "http://book.zongheng.com/chapter/1128608/66171932.html" for i in range(1,100): html = requests.get(url=next_url) e = etree.HTML(html.content) title = e.xpath('//div[@class="title_txtbox"]/text()')[0] text = '\n'.join(e.xpath('//p/text()')) next_url = e.xpath('//div/a[text()="下一章"]/@href')[0] with open(f'国公凶猛.txt','a') as f: f.write(title+'\n') f.writelines(text) print(title,'下载成功') time.sleep(0.5)