代码之家  ›  专栏  ›  技术社区  ›  Til Hund

PDF下载脚本仅下载部分PDF,速度较慢

  •  0
  • Til Hund  · 技术社区  · 6 年前

    我想从以下网站下载一些免费的PDF(旧报纸的副本) this 奥地利国家图书馆网站 wget 使用下面的bash脚本:

    #!/bin/bash
    #A script to download issues of the Leipziger Zeitung (1814-1857)
    
    for year in {14..57}; do
      DATES=$(curl -sS "http://anno.onb.ac.at/cgi-content/anno?aid=lzg&datum=18$year" | gawk 'match($0, /datum=([^&]+)/, ary) {print ary[1]}' | xargs echo)
      for date in $DATES; do
        echo "Downloading for $date"
        curl "http://anno.onb.ac.at/cgi-content/anno_pdf.pl?aid=lzg&datum=$date" -H 'Connection: keep-alive' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8' -H 'DNT: 1' -H "Referer: http://anno.onb.ac.at/cgi-content/anno?aid=lzg&datum=$date" -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: en-US,en;q=0.9' --compressed
        wget -A pdf -nc -E -nd --no-check-certificate --content-disposition http://anno.onb.ac.at/pdfs/ONB_lzg_$date.pdf
      done
    done
    

    我发现脚本只下载周一和周六的问题(如果没有,则下载周二的问题,如果有,则下载周日的问题),而不下载一周内的其余问题,请参见下面的bash输出。

    Downloading for 18140228
    Downloading for 18140402
    Downloading for 18140404
    Downloading for 18140409
    Downloading for 18140412
    Downloading for 18140416
    Downloading for 18140418
    Downloading for 18140423
    Downloading for 18140425
    Downloading for 18140430
    

    在过去,并非每一天或每一个月都会出版一期,也不是今天就有。然而,如果你比较 this 1814年的日历,例如四月,您会发现脚本每周只下载两期。它下载了1814年4月4日和4月9日的期刊,但没有下载4月5日至4月7日的可用期刊。1814年4月的其他几周以及1814年至1857年间相关时间范围内的任何其他可用月份都是如此。

    我对编写脚本很陌生,并且在当前的脚本中得到了帮助(参见 this 问题),所以我不知道如何下载所有可用的问题。

    此外,我还测量了 time curl命令的执行时间为3到5秒。有没有办法加快脚本的速度?

    2 回复  |  直到 6 年前
        1
  •  1
  •   df778899    6 年前

    浏览其中一个年度索引页面的页面源(例如。 http://anno.onb.ac.at/cgi-content/anno?aid=lzg&datum=1814 )看起来像 match() 只是挑选第一个 datum 在原始HTML的每一行上。

    更改 gawk 要使用的命令 split() 而是选择了所有匹配项:

    gawk 'split($0, t, /datum=[^&]+/, ary) {for (i=1; i in ary; i++) print substr(ary[i],7)}'
    

    (与往常一样 awk 以及它的后代,有许多其他方法可以做到这一点)。

    Downloading for 18140228
    Downloading for 18140402
    Downloading for 18140404
    Downloading for 18140405
    Downloading for 18140406
    Downloading for 18140407
    Downloading for 18140409
    Downloading for 18140412
    Downloading for 18140413
    Downloading for 18140414
    Downloading for 18140416
    

    要加快速度,请运行 wget 在后台似乎很有效:

    wget -A pdf -nc -E -nd --no-check-certificate --content-disposition http://anno.onb.ac.at/pdfs/ONB_lzg_$date.pdf &
    

    -如果不熟悉,请注意 & 在那里的尽头。

    我认为这需要更多的工作来限制每次运行的下载数量,但在一次测试中,这导致大约10-12次下载同时运行 ps -ef | grep wget 在另一个会话上。

        2
  •  0
  •   enxio27    6 年前

    您是否对照可用内容检查了下载的内容?似乎很多问题根本无法获得,尤其是周五和周日(可能没有在这两天出版?),至少在我检查的这些年里,在某些情况下,整整几个月都不见了。顺便说一下,这个项目很有趣。