zoukankan      html  css  js  c++  java
  • 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, '
    
    ')
            return con
    
        def writer(self, name, path, text):
            write_flag = True
            with open(path, 'a', encoding='utf-8') as f:
                f.write(name+'
    ')
                f.writelines(text)
                f.write('
    
    ')
    
    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)+'
    ')
            sys.stdout.flush()
        print('下载完成')
  • 相关阅读:
    ORACLE表空间操作(转)
    ORA12514错误解决方法
    ETL工具 kettle问题
    改变oracle端口号
    查询SQL Server中所有数据库的数据文件位置
    使用URTracker构建企业IT服务平台
    那些年,我做共享软件(2)
    那些年,我做共享软件(1)
    那些年,我做共享软件(3)完结
    浅析缺陷管理系统URTracker
  • 原文地址:https://www.cnblogs.com/LouisZJ/p/8663173.html
Copyright © 2011-2022 走看看