###定义一个获取url的函数,获取url。
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.90 Safari/537.36'}
def get_url():
r = requests.get('http://book.sbkk8.com/gushihui/taijiaogushi/',headers= headers)
selector = parsel.Selector(r.text)
url_s = selector.css('.mulu > ul > li > a::attr(href)').getall()
for url in url_s:
url = 'http://book.sbkk8.com/'+ url
#print(url)
download(url)
###定义一个下载函数
def download(url):
r = requests.get(url,headers = headers)
r.encoding = r.apparent_encoding
selector = parsel.Selector(r.text)
title = selector.css('#maincontent > h1::text').get()
content = selector.css('#content::text').getall()
text = ""
for i in content:
text = text + i.strip()+'\n'
path = 'C:\\Users\\yimin.lu\\Desktop\\胎教故事.txt' ###这个是文件存放路径,可以自行修改
with open(path,mode='a',encoding='utf-8') as f:
f.write(title)
f.write('\n')
f.write(text)
f.write('**************************************************************************'*3)
f.write('\n')
print("由"+url +"获取。" + title + " 下载成功")