url = "https://www.17k.com/chapter/108821/3148523.html"
def book_spider():
import requests
from bs4 import BeautifulSoup
import time
headers = {"user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"}
while True:
global url
resp = requests.get(url, headers=headers).content
time.sleep(1)
html = BeautifulSoup(str(resp, "utf-8"), "lxml")
a_tag = html.find_all("a", class_="nextChapter")
if len(a_tag) != 2:
break
next_tag = a_tag[0]["href"]
url = "https://www.17k.com" + next_tag
for tag in html.find_all("div", class_="readAreaBox content"):
title = tag.find_all("h1")
with open('小说.txt', 'a+', encoding="utf-8") as f:
f.write(title[0].string + "\n")
content = tag.find_all("p")
for i in content:
if len(i) == 0 or len(i) == 3:
continue
else:
if len(i.string) > 90:
with open('小说.txt', "a+", encoding="utf-8") as f:
f.writelines("\t%s\n%s" % (i.string[:90], i.string[90:]))
else:
with open('小说.txt', "a+", encoding="utf-8") as f:
f.writelines("\t%s\n" % (i.string))
book_spider()