我想用下面的bash脚本从奥地利国家图书馆的这网站下载一些免费下载pdfs (一份旧报纸的副本) wget:
#!/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年的这日历,例如四月,您会发现脚本每周只下载两期。它下载4月4日、1814年和4月9日的一期,但不从4月5日至4月7日下载现有的出版物。1814年4月的其他几周和1814年至1857年期间的任何其他可用月份都是如此。
我刚开始编写脚本,并帮助编写了当前的脚本(参见这里的这问题),所以我不知道如何让它下载所有可用的问题。
另外,我用time测量了curl命令的执行时间在3到5秒之间。有办法加速剧本吗?
发布于 2018-05-13 09:46:06
从浏览其中一个年度索引页(例如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 &&。我认为这需要更多的工作来限制在任何时候运行的下载的数量,但是在一个测试中,从另一个会话中查看ps -ef | grep wget,这导致了大约10到12个下载一起运行。
发布于 2018-05-11 18:27:37
您是否检查过下载的内容是否与可用的内容相对应?似乎有许多问题根本没有,特别是星期五和星期日(也许那些日子没有出版?),至少在我查过的几年里,而且在某些情况下,整个月都不见了。顺便说一句,这个项目很有趣。
https://stackoverflow.com/questions/50276590
复制相似问题