import requests
from requests_html import HTMLSession
def get_txt(url):
session = HTMLSession()
r = session.get('http://m.kanxiaoshuo8.com/{}'.format(url))
r.encoding = 'utf-8'
content = r.html.xpath("//div[@id='chaptercontent']", first=True).text
title = r.html.xpath("/html/body/header/span", first=True).text
title = title.replace(' ', ' ').replace('吞噬古帝', '').strip()
print(title)
content = content.replace('『加入书签,方便阅读』', '').strip()
with open("test.txt", 'a+', encoding='utf-8') as file:
file.write(title + "\n")
file.write(content + "\n")
file.close()
# print(r.text)
url = r.html.xpath("/html/body/p[2]/a[3]/@href", first=True)
if url:
get_txt(url)
if __name__ == '__main__':
get_txt("77_77563/6521484.html")
只适用网址:http://m.kanxiaoshuo8.com/
改一下可以加多线程,把整个网站上的小说爬取下来