欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

元尊 python

程序员文章站 2022-06-15 10:30:08
元尊 python直接上源码// An highlighted blockimport requestsimport sysfrom bs4 import BeautifulSoup"""下载笔趣阁小说元尊"""class Downloader(object): def __init__(self): self.server = 'http://www.biqu.ge/' self.target = 'http://www.bi...

元尊 python

直接上源码
// An highlighted block
import requests
import sys
from bs4 import BeautifulSoup

"""
下载笔趣阁小说元尊


"""





class Downloader(object):

    def __init__(self):
        self.server = 'http://www.biqu.ge/'
        self.target = 'http://www.biqu.ge/0/214/'
        self.names = []  # 存放章节名
        self.url = []  # 存放章节链接
        self.nums = 0  # 章节数

    def get_download_url(self):
        req = requests.get(url=self.target).content.decode('utf-8')
        html = req
        div_bf = BeautifulSoup(html, 'lxml')
        div = div_bf.find_all('div', id="list")
        a_bf = BeautifulSoup(str(div[0]), 'lxml')
        a = a_bf.find_all('a')
        self.nums = len(a[10:])  # 删除不必要的章节
        for each in a[10:]:
            self.names.append(each.string)
            self.url.append(self.server + str(each.get('href')))

    def get_contents(self,target):
        req = requests.get(url=target)
        html = req
        bf = BeautifulSoup(html.content, features='lxml')
        texts = bf.find_all('div', class_="acontent")
        texts = texts[0].text.replace('<br/>', '\n\n')
        return texts

    def writer(self,name, path, text):
        writer_flag = True
        with open(path, 'a', encoding='utf-8') as f:
            f.write(name + '\n')
            f.writelines(text)
            f.write('\n\n')


if __name__ == '__main__':
    dl = Downloader()
    dl.get_download_url()
    print("元尊开始下载")
    for i in range(dl.nums):
        for i in range(dl.nums):
            dl.writer(dl.names[i], '元尊.txt', dl.get_contents(dl.url[i]))
            sys.stdout.write("  已下载:%.3f%%" % float(i / dl.nums) + '\r')
            sys.stdout.flush()

    print('下载完成')

本文地址:https://blog.csdn.net/csdnzhaozhao/article/details/107648585

相关标签: 爬虫 python