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

[网络连接] 【已解决】批处理如何批量下载网页链接中的pdf文件?

[复制链接]
发表于 2018-2-3 19:59:25 | 显示全部楼层
回复 15# 775405984



跟性别与专业无关。这个有个医学生的笔记,好像是个妹子。好多技术类的网站都转载过她的学习笔记。
这个学习能力,笔记能力,组织能力真的是让我惊叹。

    https://woaielf.github.io/2017/06/13/python3-all/
发表于 2018-2-3 20:05:05 | 显示全部楼层
回复 16# codegay


授人以鱼不如授人以渔,道理我都懂。。。
发表于 2018-2-3 22:39:52 | 显示全部楼层
回复 17# 775405984


    https://pan.baidu.com/s/1dGQORIh
发表于 2018-2-4 21:26:34 | 显示全部楼层
四线程+队列,Mojo 本身支持多线程,不过还没学会
  1. =info
  2.     4线程+队列
  3.     523066680@163.com
  4. =cut

  5. use Modern::Perl;
  6. use Encode;
  7. use threads;
  8. use threads::shared;
  9. use Thread::Queue;
  10. use File::Basename;
  11. use URI::Escape;
  12. use Mojo::UserAgent;
  13. STDOUT->autoflush(1);

  14. my @ths;
  15. my $que = Thread::Queue->new();    # A new empty queue
  16. my $link = "http://pmmp.cnki.net/OperatingDiscipline/Details.aspx?id=";
  17. my @mission = map { sprintf "%s%04d", $link, $_ } (1 .. 2150);

  18. #创建线程
  19. grep { push @ths, threads->create( \&thread_func, $_ ) } ( 0..3 );

  20. $que->enqueue( @mission );
  21. $que->end();
  22. grep { $_->join() } @ths;

  23. exit;

  24. sub thread_func
  25. {
  26.     my ( $id ) = shift;
  27.     my ($link, $file, $res, $dom);

  28.     my $ua = Mojo::UserAgent->new();
  29.     $ua = $ua->max_redirects(5);

  30.     while (defined(my $link = $que->dequeue()))
  31.     {
  32.         $file = basename( $link );
  33.         $res = $ua->get( $link )->result;
  34.         $res->body =~/(http.*?.pdf)/;
  35.         $link = encode('gbk', decode('utf8', uri_unescape($1)));
  36.         say $link;
  37.     }
  38. }
复制代码
结果见附件
发表于 2018-2-9 21:53:25 | 显示全部楼层
回复 18# ivor

     谢谢~~~~
发表于 2018-2-19 22:10:58 | 显示全部楼层
本帖最后由 ivor 于 2018-2-20 10:19 编辑

换了个思路,采用list弹出元素的方式,发现还是很方便的哈,效率主要还是看服务器处理的速度。
10个线程够这下速度够快了吧。。。
  1. # coding:utf-8
  2. # 10线程
  3. #

  4. import bs4
  5. import urllib.request as url
  6. import threading
  7. import time


  8. s = time.time()
  9. pdfUrl = []
  10. numList = ['{:0>4}'.format(i) for i in range(1, 2150)]
  11. def getPdfUrl(threadKey = 'default'):
  12.     web_site = r'http://pmmp.cnki.net/OperatingDiscipline/Details.aspx?id=%s'
  13.     while len(numList):
  14.         num = numList.pop()
  15.         try:
  16.             req = url.urlopen(web_site % num)
  17.             soup = bs4.BeautifulSoup(req,'html.parser')
  18.             for i in soup.find_all('a'):
  19.                 if i.string == '全文下载':
  20.                     pdf = url.unquote(i.get('href'))
  21.                     pdfUrl.append(pdf + '\n')
  22.                     print("Thread[%s]: %s" % (threadKey,pdf))
  23.                     break
  24.             
  25.         except:
  26.             print("服务器错误!   当前id=%s" % num)
  27.             
  28.     print("Thread[%s]: End!!" % threadKey)
  29.     return

  30. def writeList(pdfLink):
  31.     with open("list.txt", "w") as file:
  32.         file.writelines(pdfLink)

  33. #线程实体list
  34. t = ['t1','t2','t3','t4','t5','t6','t7','t8','t9','t10']
  35. for i in t:
  36.     i = threading.Thread(target=getPdfUrl,args=(i,))
  37.     i.start()

  38. while True:
  39.     time.sleep(1)
  40.     if threading.active_count() == 1:
  41.         writeList(pdfUrl)
  42.         print("\n\n耗时: %f 秒" % (time.time() - s))
  43.         break
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-3-18 04:54 , Processed in 0.010617 second(s), 7 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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