当前位置 博文首页 > li5672的专栏:使用python下载一部小说

    li5672的专栏:使用python下载一部小说

    作者:[db:作者] 时间:2021-08-30 16:02

    # -*- coding:UTF-8-*-
    from bs4 import BeautifulSoup
    import requests
    import os
    def mkdir(path):
        path = path.strip()
        isExists = os.path.exists(path)
    
        if not isExists:
            os.makedirs(path)
            print(path + ' 创建成功')
            return True
        else:
            print(path + ' 目录已存在')
            return False
    
    def writer(name, text):
        file_name = 'D:\\MySpace\\github\\py-demo\\book\\' + name+'.txt'
        with open(file_name, 'a', encoding='utf-8') as f:
            f.write(file_name + '\n')
            f.writelines(str(text).replace('\xa0'*8, '\n\n').replace('<br/>', ''))
            f.write('\n\n')
    
    
    def download_text(obj):
        target = 'http://www.biqukan.com'
        req = requests.get(target+obj['url'])
        html = req.text
        bf = BeautifulSoup(html)
        texts = bf.find_all('div', class_='showtxt')
        writer(obj['name'], texts)
    
    
    if __name__ == "__main__":
        target = 'https://www.biqukan.com/1_1094/'
        req = requests.get(url=target)
        req.encoding = 'gb18030'
        html = req.text
        div_bf = BeautifulSoup(html)
        div = div_bf.find_all('div', class_='listmain')
        a_bf = BeautifulSoup(str(div[0]))
        a = a_bf.find_all('a')
        for each in a:
            obj = {}
            obj['name'] = each.string
            obj['url'] = each.get('href')
            download_text(obj)
    
    

    效果图

    cs
    下一篇:没有了