英文:
Download automation and monkey swinging to next page
问题
以下是您要求的代码部分的中文翻译:
#!/bin/bash
# 用法: bash myDown.sh example.org
# 获取网址
START=$1
while [[ $# -gt 0 && -z $START ]]; do
# 获取下载链接
DOWN=$(wget -O- "$START" | grep "download.php?id=*" | grep -io 'href=["'\'']*[^"'\''']*["'\'']' | cut -d\" -f2)
# 运行下载
wget -O "$START" "$DOWN"
# 获取下一页
NEXT=$(wget -O- "$START" grep -m1 "\"next\"" | grep -io 'href=["'\'']*[^"'\''']*["'\'']' | cut -d\" -f2)
# 转到下一页
START=$NEXT
done
请注意,上述代码可能包含特殊字符,如引号和斜杠,这些字符可能会影响代码的执行。确保在复制和粘贴时没有损坏字符。如果您有进一步的问题或需要帮助改进代码,请告诉我。
英文:
So here's what I got at the moment
#!/bin/bash
#USAGE: bash myDown.sh example.org
#get url
START=$1
while [[ $# -gt 0 && -z $START ]]; do
#get download url
DOWN=$(wget -O- "$START" | grep "download.php?id=*" | grep -io 'href=['"'"'"][^"'"'"']*['"'"'"]' | cut -d\" -f2)
#run download
wget -O "$START" "$DOWN"
#get next page
NEXT=$(wget -O- "$START" grep -m1 "\"next\"" | grep -io 'href=['"'"'"][^"'"'"']*['"'"'"]' | cut -d\" -f2)
#goto next
START=$NEXT
done
So in my mind this should get the response from the url, grep it for $DOWN and $NEXT, run $DOWN and monkeyswing to NEXT as next while rounds START.
When running it against a downloaded example.php from the site in question the greps work fine, manual wget via terminal with the DOWN url works just as well as clicking the download button on the site would, but something's not working as imagined.
Can you see what I did wrong or how I could do things better?
I initially thought about doing a recursive wget but the download is all via a "central" /download.php?id=... and the id's aren't all incremental for the respective books so I chose to monkeyswing via grepping from the next button.
When the last chapter is reached there is no next button so [ -z $START ] should terminate, but it isn't even running so far
Kinda lost here.
Thanks!
$ bash -x myDown.sh "https://www.example.html"
+ START=example.html
+ [[ 1 -gt 0 ]]
+ [[ -z example.html ]]
+ :
site substituted
答案1
得分: 0
你可以从跟踪中看到,循环体从未被执行。
-z $START
如果START
为空,则为真。然而,START
被设置为example.html
。因此,-z $START
为假,而_while_的条件也为假。
你必须否定条件:
[[ $# -gt 0 && -n $START ]]
-n
用于测试是否_不为空_。
英文:
You can see from the trace that the loop body is never entered.
-z $START
is true, if START
is empty. However, START
is set to example.html
. THerefore -z $START
is false and the condition for the while is also false.
You have to negate the condition:
[[ $# -gt 0 && -n $START ]]
-n
tests for not empty.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论