菜鸡源码,专注精品下载!
当前位置:首页 > 建站教程 > 建站知识

Python爬虫下载源码的原创力文库

发布时间:2024-01-05  栏目:建站知识   浏览:   分类:python教程 python爬虫 Python源码

原创力文库Python爬虫下载源码是一种利用Python编程语言编写的网络爬虫程序,用于从原创力文库网站上自动下载文档。这种爬虫程序通常包括以下几个部分:1. 网络请求模块,用于向原创力文库网站发送请求并获取网页内容;2. 网页解析模块,用于解析网页内容并提取所需信息;3. 文件下载模块,用于将提取到的信息保存到本地文件中。通过使用这种爬虫程序,用户可以方便地从原创力文库网站上批量下载文档,节省时间和精力。

#!/usr/bin/python#-*-coding:UTF-8-*-importreimportjsonimportosimportshutilimportsysimporttimeimportrequestsimportimg2pdffromPILimportImagefromalive_progressimportalive_barfromrequests.exceptionsimportSSLErrorpng_headers={'User-Agent':'Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/106.0.0.0Safari/537.36','Referer':'https://max.book118.com/',}defdown_pngs(pageIndex):print(pngs[pageIndex])down_url='https://view-cache.book118.com'+pngs[pageIndex]print(down_url)res=requests.get(url=down_url)try:png=res.contentwithopen(os.path.join(temp_dir,str(pageIndex)+'.jpeg'),'wb')asf:f.write(png)except:returnlogo="""______/_@)_@)\/^^\/^\/^^\__/oo\____/~''...'~\/'\''~''~~'-'\_/'.'.~.~.~..'~|/'\~~..''''.''''~\_('_'_'_'_'_'_'_'_':'\_/''.''.'..'''.~\_~V~V~V~V\~\'''~'''~`~''~\_/\~/\~/\~/\~/|/'''_'~'''~''__'..\_<--------.---.--/'''/''\_''':~;;'''/'';\;''''''''~\_\~'..:.::~.:./_'''_''\_':'''_:_''/''_''_\:_''''#''..\/\/\/~/\~''~~~~~O~~\-~`---~~~----\(_)(_)(_)/~~~'~\(_)(_)(_)\_~_~_~_~_~/&#732;¤1---------------------------------------------------------------------------------------------by:bean0283"""print(logo)whileTrue:url=input('请输入原创力文库url:')url=url.split('?')[0]print('下载地址:',url)temp_dir=url.split('/')[-1]#删除老的临时文件夹并新建临时文件夹ifos.path.exists(temp_dir):shutil.rmtree(temp_dir)os.mkdir(temp_dir)print('开始下载HTML...',end='')try:response=requests.get(url=url)except(SSLError):print("\n\033[31m不要使用dai理软件-------\033[0m")print("\033[31m请关闭dai理软件后重新运行程序\033[0m")print("\033[31m请关闭vpn软件后重新运行程序\033[0m")sys.exit(0)page=response.textprint('成功.\n开始解析HTML...',end='')title=re.search('title:(.*),',page).group(1).replace("'","")view_token=re.search('view_token:(.*)\'',page).group(1).replace("'","")filetype=re.search('format:(.*)\'',page).group(1).replace("'","")senddate=re.search('senddate:(.*),',page).group(1).replace("'","")aid=re.search('aid:(.*),//解密后的id',page).group(1)actual_page=int(re.search('actual_page:(.*),',page).group(1))#真实页数preview_page=int(re.search('preview_page:(.*),',page).group(1))#可预览页数ifactual_page>preview_page:print("\n\033[31m该文档为限制文档,无法下载全部内容,请用vip+rmb,该问题无需反馈给开发者,他也不会解决!!\033[0m\n")output=title#输出文件(夹)print('解析成功.')print('文档标题:',title)#接口每次会返回6个下载page地址list_pn=list(range(1,preview_page+1,6))#print(pngs)iffiletype=='pdf':pngs={}print('解析到pdf文档,准备开始解析下载..',end='')print('解析成功.\n正在获取pngs下载地址...')print('受接口限制,2s访问一次,请耐心等待所有接口信息返回')withalive_bar(len(list_pn),title='ing...')asbar:forpninlist_pn:bar()down_page_url='https://openapi.book118.com/getPreview.html?project_id=1&aid={}&view_token={}&page={}&_={}'.format(aid,view_token,pn,str(int(time.time())))jsonpReturn=requests.get(url=down_page_url)page=re.search('jsonpReturn\((.*)\)',jsonpReturn.text).group(1)data_temp=json.loads(page)['data']#print(data_temp)pngs.update({x:data_temp[x]forxindata_temp})#这里有个bug,若返回值的url为空时,这里不会报错,但会造成下载png时异常,暂时没有考虑处理ifpn!=list_pn[-1]:time.sleep(2)print('\n开始下载jpg(s)...')pagenums=list(range(1,len(pngs)+1))withalive_bar(len(pagenums),title='ing...')asbar:foriinrange(len(pagenums)):bar()down_url="https:"+pngs[str(i+1)]request=requests.get(url=down_url,headers=png_headers)try:page=request.contentwithopen(os.path.join(temp_dir,str(pagenums[i])+'.jpeg'),'wb')asf:f.write(page)except:continueprint('\n开始合并图片成PDF...',end='')file_imgs=[os.path.join(temp_dir,str(i)+'.jpeg')foriinpagenums]#不用以下代码会使img2pdf报错forimg_pathinfile_imgs:withopen(img_path,'rb')asdata:img=Image.open(data)#将PNG中RGBA属性变为RGB,即可删掉alpha透明度通道img.convert('RGB').save(img_path)withopen(output+'.pdf','wb')asf:f.write(img2pdf.convert(file_imgs))shutil.rmtree(temp_dir)print('下载成功.')print('保存到'+output+'.pdf')eliffiletypein['docx','doc']:pngs={}print('解析到{}文档,准备开始解析下载..'.format(filetype),end='')print('解析成功.\n正在获取pngs下载地址...')print('受接口限制,2s访问一次,请耐心等待所有接口信息返回')withalive_bar(len(list_pn),title='ing...')asbar:forpninlist_pn:down_page_url='https://openapi.book118.com/getPreview.html?&project_id=1&aid={}&t={}&view_token={}&page={}&_={}'.format(aid,senddate,view_token,pn,str(int(time.time())))jsonpReturn=requests.get(url=down_page_url)page=re.search('jsonpReturn\((.*)\)',jsonpReturn.text).group(1)data_temp=json.loads(page)['data']#print(data_temp)bar()pngs.update({x:data_temp[x]forxindata_temp})ifpn!=list_pn[-1]:time.sleep(2)print('\n开始下载jpg(s)...')pagenums=list(range(1,len(pngs)+1))withalive_bar(len(pagenums),title='ing...')asbar:foriinrange(len(pagenums)):down_url="https:"+pngs[str(i+1)]request=requests.get(url=down_url,headers=png_headers)bar()try:page=request.contentwithopen(os.path.join(temp_dir,str(pagenums[i])+'.jpeg'),'wb')asf:f.write(page)except:continueprint('\n开始合并图片成PDF...',end='')file_imgs=[os.path.join(temp_dir,str(i)+'.jpeg')foriinpagenums]forimg_pathinfile_imgs:withopen(img_path,'rb')asdata:img=Image.open(data)#将PNG中RGBA属性变为RGB,即可删掉alpha透明度通道img.convert('RGB').save(img_path)withopen(output+'.pdf','wb')asf:f.write(img2pdf.convert(file_imgs))shutil.rmtree(temp_dir)print('下载成功.')print('保存到'+output+'.pdf')else:print('不支持的参数.文件类型:',filetype)temp_=os.path.realpath(sys.argv[0])os.startfile(os.path.dirname(temp_))print("执行完成,继续下载请黏贴url,结束请关闭窗口\n")
评论
建站知识
建站知识
使用技巧
调试安装
运营推广