import requests
import lxml
import re

headers = {

    'user-agent': 'User-AgentMozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36'
}
print ('本脚本仅适用于138看书网：https://www.13800100.com/')
#url_list = 'https://www.13800100.com/list/72262/'
url_list = (input('粘贴小说目录url，必须是小说目录，小说首页不支持\n'))
downurl = 'https://www.13800100.com/article/'
url_list = requests.get(url_list)
text_list = url_list.text
text_title = re.findall(r'<div class="cate-tit">(.*?)</h2>',text_list,re.S)[0]
text_title = text_title.replace('\r\n','')
text_title = text_title.replace('<h2>','')
text_title = text_title.replace(' ','')
text_list_info = re.findall(r'<div class="bd">.*?</div>',text_list,re.S)[0]
text_list_info = re.findall(r'<a href="/article/(.*?)" class="name">(.*?)</a>',text_list)

for i in text_list_info:
    list = i[0]
    name = i[1]
    download = downurl + list
    download_info = requests.get(url = download,headers=headers)
    html=download_info.text
    html_info = re.findall(r'<div ondragstart="return false" oncopy="return false;" oncut="return false;" oncontextmenu="return false" class=".*?">(.*?)</div>',html,re.S)[0]
    html_info = html_info.replace ('						　　','')
    html_info = html_info.replace ('<br/>',('\n'))
    html_info = html_info.replace ('　　','')
    print (name)
    with open ('%s.txt' % text_title,'a+',encoding = 'utf-8')as f:
        f.write('                                                '+ name + '\n')
        f.write('\n')
        f.write(html_info + '\n')
        f.write('\n')
print ('下载完成')
