【问题标题】:PDF download script downloads only some PDF and is slowPDF下载脚本只下载一些PDF并且速度很慢
【发布时间】:2018-05-10 15:37:54
【问题描述】:

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

#!/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

在过去,并非每天或每月都会发布或今天可用。但是,如果您比较 1814 年的 this 日历,例如四月,您会发现该脚本每周只下载两期。它下载 1814 年 4 月 4 日和 4 月 9 日的期刊,但不下载 4 月 5 日至 4 月 7 日的可用期刊。对于 1814 年 4 月的其他周以及 1814 年至 4 月 9 日相关时间范围内的任何其他可用月份都是如此1857.

我是编写脚本的新手,对当前的脚本有帮助(请参阅此处的this 问题),所以我不知道如何下载所有可用的问题。

另外,我用time 测量到 curl 命令需要 3 到 5 秒才能执行。有没有办法加速脚本?

【问题讨论】:

    标签: bash pdf download wget


    【解决方案1】:

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

    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

    【讨论】:

    • 太棒了,d778899,这成功了!
    • 为什么curl实际上需要这么多时间(3到5秒)才能执行?
    • 看起来大部分时间是来自 ONB Web 服务器的响应时间。例如,使用 Chrome 中的网络检查器(F12,然后是“网络”选项卡),anno.onb.ac.at/cgi-content/… 的时间仅为 4.66 秒。
    【解决方案2】:

    您是否根据可用内容检查了已下载的内容?似乎很多问题根本不可用,特别是周五和周日(也许那些日子没有出版?),至少在我检查的这些年里,在某些情况下,整月都不见了。顺便说一句,有趣的项目。

    【讨论】:

    • 感谢您的回答,enxio27。我应该更清楚地说明有几个月,还有很多天不见了(要么还不可用,要么在过去的日子里从未发回)。但是,上面的脚本错过了可用的问题,例如,从 1814 年 4 月 3 日到 4 月 8 日的问题,与下载问题的上述输出和 1814 年 4 月的月份here 相比。
    猜你喜欢
    • 1970-01-01
    • 2016-12-18
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 2022-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多