找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 13524|回复: 0

[原创代码] Python科幻小说下载器

[复制链接]
发表于 2019-5-12 15:54:50 | 显示全部楼层 |阅读模式
使用Python2.7.13
  1. # encoding:utf-8
  2. import urllib2
  3. import re

  4. beginning="http://www.kehuan.net.cn"

  5. #解析目录或文字
  6. def get(address,name):
  7.     web=urllib2.urlopen(beginning+address).read()
  8.     basis=re.findall(re.compile(r'(?<=<dd><a href=").+?(?=">)'),'r"'+web+'"')
  9.     if basis != []:
  10.         fout=open(name.decode('utf-8')+'.log',"a+")
  11.         fout.write("            "+name+'\n\n')
  12.         fout.close()
  13.         for i in basis:
  14.             get(i,name)
  15.     else:
  16.         if re.search(re.compile(r'(?<=<title>).+?(?=</title>)'),'r"'+web+'"'):
  17.             title=re.search(re.compile(r'(?<=<title>).+?(?=</title>)'),'r"'+web+'"').group(0)
  18.         else:
  19.             title='runtime wrong'
  20.         if re.findall(re.compile(r'(?<=<p>).+?(?=</p>)'),'r"'+web+'"'):
  21.             article=re.findall(re.compile(r'(?<=<p>).+?(?=</p>)'),'r"'+web+'"')
  22.         else:
  23.             article=' '
  24.         fout=open(name.decode('utf-8')+'.doc',"a+")
  25.         fout.write("        "+title+'\n')
  26.         for i in article:
  27.             fout.write("    ")
  28.             j=0
  29.             while j < len(i):
  30.                 if i[j]!='&':
  31.                     fout.write(i[j])
  32.                 elif i[j+1]=='h' and i[j+2]=='e' and i[j+3]=='l' and i[j+4]=='l' and i[j+5]=='i' and i[j+6]=='p' and i[j+7]==';':
  33.                     fout.write('…')
  34.                     j=j+7
  35.                 elif i[j+1]=='m' and i[j+2]=='i' and i[j+3]=='d' and i[j+4]=='d' and i[j+5]=='o' and i[j+6]=='t' and i[j+7]==';':
  36.                     fout.write('·')
  37.                     j=j+7
  38.                 elif i[j+1]=='l' and i[j+2]=='d' and i[j+3]=='q' and i[j+4]=='u' and i[j+5]=='o' and i[j+6]==';':
  39.                     fout.write('“')
  40.                     j=j+6
  41.                 elif i[j+1]=='r' and i[j+2]=='d' and i[j+3]=='q' and i[j+4]=='u' and i[j+5]=='o' and i[j+6]==';':
  42.                     fout.write('”')
  43.                     j=j+6
  44.                 elif i[j+1]=='l' and i[j+2]=='s' and i[j+3]=='q' and i[j+4]=='u' and i[j+5]=='o' and i[j+6]==';':
  45.                     fout.write('‘')
  46.                     j=j+6
  47.                 elif i[j+1]=='r' and i[j+2]=='s' and i[j+3]=='q' and i[j+4]=='u' and i[j+5]=='o' and i[j+6]==';':
  48.                     fout.write('’')
  49.                     j=j+6
  50.                 elif i[j+1]=='m' and i[j+2]=='d' and i[j+3]=='a' and i[j+4]=='s' and i[j+5]=='h' and i[j+6]==';':
  51.                     fout.write('—')
  52.                     j=j+6
  53.                 elif i[j+1]=='q' and i[j+2]=='u' and i[j+3]=='o' and i[j+4]=='t' and i[j+5]==';':
  54.                     fout.write('"')
  55.                     j=j+5
  56.                 elif i[j+1]=='n' and i[j+2]=='b' and i[j+3]=='s' and i[j+4]=='p' and i[j+5]==';':
  57.                     fout.write('\n')
  58.                     j=j+5
  59.                 elif i[j+1]=='l' and i[j+2]=='t' and i[j+3]==';':
  60.                     fout.write('<')
  61.                     j=j+3
  62.                 elif i[j+1]=='g' and i[j+2]=='t' and i[j+3]==';':
  63.                     fout.write('>')
  64.                     j=j+3
  65.                 else:
  66.                     print i[j:j+7]
  67.                 j=j+1
  68.             fout.write('\n')
  69.         fout.write('\n')
  70.         fout.close()


  71. #主函数
  72. web=urllib2.urlopen("http://www.kehuan.net.cn/author/liucixin.html").read()
  73. result=re.findall(re.compile(r'(?<=<li><a href=").+?(?=">)'),'r"'+web+'"')
  74. name=re.findall(re.compile(r'(?<=.html">).+?(?=</a>)'),'r"'+web+'"')
  75. for i in range(0,len(name)):
  76.     if re.search(re.compile(r'(?<=<strong>).+?(?=</strong>)'),'r"'+name[i]+'"'):
  77.         name[i]=re.search(re.compile(r'(?<=<strong>).+?(?=</strong>)'),'r"'+name[i]+'"').group(0)
  78. for i in range(9,len(result)):
  79.     get(result[i],name[i])

复制代码
速度有点慢,请谅解

评分

参与人数 1PB +6 技术 +1 收起 理由
523066680 + 6 + 1 支持原创

查看全部评分

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-16 22:22 , Processed in 0.017286 second(s), 9 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表