栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

系统将跳转到其他网站进行下载

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

系统将跳转到其他网站进行下载

import requests
from bs4 import BeautifulSoup
from urllib import parse
import time

class bqg(object):
    def bqg_search(self):

        keyword = input("请输入你想搜索的小说关键字或者小说的名字:")
        data = {
            # 'searchtype':'articlename',
            'searchkey': keyword
            # 'action':'login',
            # 'submit':' 搜  索 '
        }
        self.data=parse.urlencode(data,encoding='ansi')
        res = requests.get(url=self.bqg_search_url, headers=self.headers, params=self.data)
        res.encoding = 'gbk'
        soup = BeautifulSoup(res.text, 'lxml')
        refer_link = soup.find('link')['href']   #获取小说的refer网址
        if(soup.find('div',id='list')!=None):     #这部分表示只能找到一本小说
            lists=soup.find('div',id='list')
            list = lists.find('dl')
            title=str(list.find('dt').string).replace('正文','')   #获取小说的名字
            dd = list.find('dd')
            first_link=refer_link+dd.find('a')['href']
            # print(first_link)
            fp=self.creat_file(title)
            print("您搜索的小说:"+title+" 已找到")
            return fp,first_link
        elif(soup.find('div',class_='novelslistss').find('li')!=None):
            page = soup.find('div', class_='pagelink')
            page_count = str(page.find('em').string).split('/')[1].strip()
            page_link = page.find('a')['href']
            name_list=[]
            link_list=[]
            for i in range(1, int(page_count) + 1):
                listl,listn=self.many_pageparse('https://www.52bqg.com/' + str(page_link).replace('1', str(i)))
                for link in listl:
                    link_list.append(link)
                for name in  listn:
                    name_list.append(name)
            count=1
            for name in name_list:
                print((str(count)+'.'+name).ljust(15))
                count+=1
            novel_index=int(input("请输入你想要下载的小说编号"))
            print("你选择的是"+' '+str(novel_index)+name_list[novel_index-1])
            print(name_list[novel_index-1]+"  即将开始下载")
            res=requests.get(link_list[novel_index-1],headers=self.headers)
            res.encoding='gbk'
            soup = BeautifulSoup(res.text, 'lxml')
            refer_link = soup.find('link')['href']
            lists = soup.find('div', id='list')
            list = lists.find('dl')
            title = str(list.find('dt').string).replace('正文', '')  # 获取小说的名字
            dd = list.find('dd')
            first_link = refer_link + dd.find('a')['href']
            # print(first_link)
            fp = self.creat_file(title)
            return fp, first_link
        elif(soup.find('div',class_='novelslistss').find('li')==None and soup.find('div',id='list')==None):
            print("对不起,您搜索的关键字:"+keyword+"  没有找到,请换一个试试吧")
            answer=input("是否取消搜索,并转用其他接口进行搜索 输入Y/N(忽略大小写)")
            if answer.startswith('Y') or answer.startswith('y'):
                # print("系统将跳转到其他网站进行下载")
                return None
            else:
                print("该网站没有找到您想看的小说,可以尝试换一部哦")
                return 1
    def many_pageparse(self,url):
            res=requests.get(url=url,headers=self.headers)
            res.encoding='gbk'
            soup=BeautifulSoup(res.text,'lxml')
            novelslistss = soup.find('div', class_='novelslistss')
            result_name = novelslistss.find('h2').string
            lis = novelslistss.find_all('li')
            link_list = []
            name_list = []
            for li in lis:
                span2 = li.find('span', class_='s2')
                a = span2.find('a')
                novel_title = a.string
                href = a['href']
                name_list.append(novel_title)
                link_list.append(href)
            return link_list,name_list
            # count = 1
            # for name in link_list:
            #     print(str(count) + ' ' + name, end='')
            #     if (count % 4 == 0):
            #         print('n')
            #     count += 1

    def get_content(self,url):
        res=requests.get(url=url,headers=self.headers)
        res.encoding='gbk'
        soup=BeautifulSoup(res.text,'lxml')
        book_name=soup.find('div',class_="bookname")
        bookname=book_name.find('h1').string     #获取章节名
        # print(bookname)
        bookname='rr'+bookname+'n'
        list_novel=[]
        contents=soup.find('div',attrs={'id':'content','name':'content'})
        novel_content=contents.strings
        for content in novel_content:
            list_novel.append(str(content))
        list_novel.insert(1,bookname.replace('《','').replace('》',''))
        novel_list=[]
        for line in list_novel:
            novel_list.append(str(line).rstrip())
        # print(novel_list[1:])
        return  novel_list[1:]

    def get_next_url(self,url):
        res=requests.get(url=url,headers=self.headers)
        res.encoding='gbk'
        soup=BeautifulSoup(res.text,'lxml')
        bottem=soup.find('div',class_='bottem')
        next_link=list(bottem.find_all('a'))[3]['href']
        return next_link

    def down_load(self):
        try:
            fp, first_link = self.bqg_search()
        except:
        # if(fp==None or first_link==None):
            print("系统操作异常,请稍后重试")
            return 0
        while(1):
            try:
                conten_list = self.get_content(first_link)
                fp.writelines(conten_list)
                link = first_link
                print("正在下载"+' '+conten_list[0].strip())
                first_link = self.get_next_url(first_link)
            except:
                fp.write("最后一次下载链接:n" + link)
                break
        print(fp.name+"已经下载完毕")
        time.sleep(5000)
        return 0

    def creat_file(self,file_name):
        fp=open(r'D:/novels/'+file_name+'.txt','a+',encoding='utf-8')
        return fp
    def main(self):
        while 1:
            self.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36'}
            self.bqg_search_url = 'https://www.52bqg.com/modules/article/search.php?searchkey='
            try:
                self.down_load()
            except:
                print("系统发生异常,请重试")
            finally:
                flage = input("您是否要继续下载小说? 请输入Y/N(忽略大小写)")
                if flage.startswith('N') or flage.startswith('n'):
                    print("欢迎下次使用")
                    break
                else:
                    self.main()

        # self.get_content('https://www.52bqg.com/book_80265/23413578.html')

if __name__ == '__main__':
    try:
        novel = bqg()
        novel.main()
    except:
        print("系统将跳转到其他网站进行下载")


 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/966322.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号