Python 爬取笔趣看小说

Posted Louiszj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python 爬取笔趣看小说相关的知识,希望对你有一定的参考价值。

# -*- coding:utf-8 -*-
from bs4 import BeautifulSoup
import requests
import sys


class DownLoader(object):
    def __init__(self):
        self.server = http://www.biqukan.com/
        self.target = http://www.biqukan.com/0_790/
        self.header = {User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebK
                                     it/537.36 (KHTML, like Gecko) Chrome/64.0.3282.119 Safari/537.36}
        self.name = []
        self.urls = []
        self.num = 0

    def get_downloader_url(self):
        req = requests.get(url=self.target, headers=self.header)
        html = req.text
        div_bf = BeautifulSoup(html, lxml)
        div = div_bf.find_all(div, class_=listmain)
        a_bf = BeautifulSoup(str(div[0]), lxml)
        a = a_bf.find_all(a)
        self.num = len(a[16:])
        for i in a[16:]:
            self.name.append(i.string)
            self.urls.append(self.server+i.get(href))

    def get_content(self, target):
        req = requests.get(target, headers=self.header)
        html = req.text
        con_bf = BeautifulSoup(html, lxml)
        con = con_bf.find_all(div, class_=showtxt)
        con = con[0].text.replace(\xa0*8, \n\n)
        return con

    def writer(self, name, path, text):
        write_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_downloader_url()
    print(《元尊》开始下载:)
    for i in range(dl.num):
        dl.writer(dl.name[i], 元尊.txt, dl.get_content(dl.urls[i]))
        sys.stdout.write("已下载:%.3f" % float(i/dl.num)+\r)
        sys.stdout.flush()
    print(下载完成)

 

以上是关于Python 爬取笔趣看小说的主要内容,如果未能解决你的问题,请参考以下文章

Python BeautifulSoup 爬取笔趣阁所有的小说

用Python爬取笔趣网小说,一次看个爽

python应用:爬虫框架Scrapy系统学习第四篇——scrapy爬取笔趣阁小说

爬取笔趣阁小说(一念永恒)

用爬虫爬取笔趣阁小说

爬取笔趣阁小说