Python简易鼠标连点器

  • 2020 年 11 月 03 日
  • 43次
  • 137 字
  • 暂无评论

import re
import requests
import sys
import os
import time


class Spider:
    def __init__(self):
        self.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4168.3 Safari/537.36'}
        self.searchUrl = 'https://www.biquge.cc/ar.php?keyWord='
        self.url = 'https://www.biquge.cc'

    def search(self, keyWord):
        req = requests.get(self.searchUrl+keyWord, headers=self.headers)
        html = req.text
        allBook = re.findall('<span class="s1">(.*?)</span>\n<span class="s2">\n<a href="(.*?)">(.*?)</a>', html)
        return allBook[::2]

    def all_chapters(self, book):
        req = requests.get(self.url+book, headers=self.headers)
        html = req.text
        try:
            allChapters = re.search('<h2 class="layout-tit">.*?</h2>(.*?)</div>', html).group()
            allChapters = re.findall('<a style="" href="(.*?)">(.*?)</a>', allChapters)
        except AttributeError:
            return '本书内容为空'
        return allChapters

    def download(self, chapter, bookName, title, path=os.getcwd()):
        if os.path.exists(bookName) is False:
            os.mkdir(bookName)
        req = requests.get(self.url+chapter, headers=self.headers)
        html = req.text
        text = re.findall('(.*?)<br />', html)
        with open(f'{path}/{bookName}/{title}.txt', 'w') as f:
            f.write('\t'*8 + title)
            f.write('\n')
            f.close()

        with open(f'{path}/{bookName}/{title}.txt', 'a') as f:
            for text in text:
                f.write(text.replace('&nbsp;', ' '))
                f.write('\n')
                # print(text.replace('&nbsp;', ' '))
            f.close()


def main():
    spider = Spider()
    n = 1
    keyWord = input('输入要搜索的内容,宁可少字也不要错字:\n')
    allBook = spider.search(keyWord)
    if allBook == '本书内容为空':
        print('本书内容为空')
        choose = input('是否继续搜索(y/n)?\n')
        if choose == 'y':
            pass
        else:
            sys.exit()

    elif len(allBook) == 0:
        print('未能找到相关内容')
        choose = input('是否继续搜索(y/n)?\n')
        if choose == 'y':
            pass
        else:
            sys.exit()

    else:
        os.system('clear')
        for book in allBook:
            print(f'{n}. {book[2]} {book[0]} ')
            n += 1
        book = int(input('输入你要下载的书籍编号:\n'))
        bookUrl = allBook[book-1][1]
        # print(allBook)
        allChapter = spider.all_chapters(bookUrl)
        n = 1
        for chapter in allChapter:
            print(str(n)+'. '+chapter[1])
            # print(chapter)
            n += 1
        downloadChapters = input('\n输入要下载的章节编号\nPS:多个章节用空格隔开或者使用"-"符号,如1 2 3和1-3都代表1到3章:\n')
        if '-' in downloadChapters:
            where = downloadChapters.index('-')
            downloadChapters = downloadChapters[: where] + ' ' + downloadChapters[where: where+1] + ' ' + downloadChapters[where+1:]

        if ' ' in downloadChapters:
            downloadChapters = re.split(' ', downloadChapters)
            downloadChapters = [i for i in downloadChapters if i != '']

        if len(downloadChapters) == 1:
            downloadChapters = [downloadChapters]

        if '-' in downloadChapters:
            where = downloadChapters.index('-')
            downloadChapters.extend([str(i) for i in range(int(downloadChapters[where-1]), int(downloadChapters[where+1]) + 1)])
            del downloadChapters[where], downloadChapters[where-1], downloadChapters[where-1]

        downloadChapters = [int(i) for i in downloadChapters]
        downloadChapters = list(set(downloadChapters))
        downloadChapters.sort()
        downloadChapters = [str(i) for i in downloadChapters]
        # print(downloadChapters)
        choose = input(f"确定下载{', '.join(downloadChapters)}章(y/n)?")
        if choose == 'y':
            path = input('输入要下载的路径,直接按回车为当前工作目录:\n')
            if path == '':
                path = os.getcwd()
            t = time.time()
            for chapter in downloadChapters:
                chapterUrl = allChapter[int(chapter) - 1][0]
                title = allChapter[int(chapter) - 1][1]
                bookName = allBook[book-1][2]
                spider.download(chapterUrl, bookName, title, path)
            print(f'区别下载完成耗时:{time.time()-t}s')
        else:
            pass

        choose = input('是否继续搜索(y/n)?\n')
        if choose == 'y':
            pass
        else:
            sys.exit()
    # Spider.all_chapters(input())


if __name__ == '__main__':
    # Spider().download('/html/141/141337/3358999_2.html','1','1')
    while True:
        main()

版权属于:RA

本文链接:https://rablog.top/2.html

本站分享的所有资源,均不得用于非法用途!



—— 暂无评论 ——

OωO