Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

BrianFD3S

macrumors member
Original poster
Feb 19, 2008
71
0
Ireland
Hi all.

I was looking around the web for a method to download all the xkcd webcomic strips at once. xkcd is available under Creative Commons, so there's no legality issue, I just can't seem to figure it out. I've found what apparently is a script on DZone - link - but I have no idea how to run it. I saved it as a plain text file in TextEdit, then changed the extension to .sh and tried to get it to execute in Terminal, but no joy. Terminal states: "Cannot execute binary file".

The code provided in the link is this:
Code:
#!/bin/bash

for i in `seq 1 449`
do
	wget http://xkcd.com/$i/
	wget `grep http://imgs.xkcd.com/comics/ index.html | head -1 | cut -d\" -f2`
	rm index.html
done

(With the sequence number updated in line with the current strip number)

If anyone could help me out, that would be great.
 
Hi all.

I was looking around the web for a method to download all the xkcd webcomic strips at once. xkcd is available under Creative Commons, so there's no legality issue, I just can't seem to figure it out. I've found what apparently is a script on DZone - link - but I have no idea how to run it. I saved it as a plain text file in TextEdit, then changed the extension to .sh and tried to get it to execute in Terminal, but no joy. Terminal states: "Cannot execute binary file".

The code provided in the link is this:
Code:
#!/bin/bash

for i in `seq 1 449`
do
	wget http://xkcd.com/$i/
	wget `grep http://imgs.xkcd.com/comics/ index.html | head -1 | cut -d\" -f2`
	rm index.html
done

(With the sequence number updated in line with the current strip number)

If anyone could help me out, that would be great.

Make sure that the permissions are set to execute. Do an "ls -la" and see what the permissions are. If they are not set to execute, the script won't run.

Run chmod filename -777 to rectify that.
 
Ok, thanks very much. I haven't run it yet, because I thought I'd first ask- how are these files going to be downloaded [Autostart of Safari?], and where are they going to get saved? I don't want c500 scripts downloading themselves onto my desktop, my screen is only 15" :eek:

ls -la xkcd.sh gives me -rwxr-xr-x
 
Ok, thanks very much. I haven't run it yet, because I thought I'd first ask- how are these files going to be downloaded [Autostart of Safari?], and where are they going to get saved? I don't want c500 scripts downloading themselves onto my desktop, my screen is only 15" :eek:

ls -la xkcd.sh gives me -rwxr-xr-x

Run the script from the directory you want the files downloaded to. It will save them all to the current working directory.

Also, Randall uses title text on every comic for extra comedic effect. If you just download the images, you will miss out on all of that. How to grab the title text as well would be a bit more difficult, but certainly not impossible.

-Lee
 
Brians-iMac:~/Desktop/xkcd [user]$ ./xkcd.sh
-bash: ./xkcd.sh: cannot execute binary file


Any ideas?
 
It does :/

To create the actual file I used TextEdit, is that right?

The text editor doesn't matter: vi, pico, etc...

The first line of the file doesn't read what I told you to put. It should be:

Code:
#!/bin/sh

for i in `seq 1 449`
do
	wget http://xkcd.com/$i/
	wget `grep http://imgs.xkcd.com/comics/ index.html | head -1 | cut -d\" -f2`
	rm index.html
done
 
Yeah, I meant I had changed it on my computer. Starting with a new file it now tells me that it doesn't recognise 'seq' as a command-

./xkcd2.sh: line 1: seq: command not found


I don't get it what's going on at all, so I really appreciate your help so far, thanks.
 
Yeah, I meant I had changed it on my computer. Starting with a new file it now tells me that it doesn't recognise 'seq' as a command-

./xkcd2.sh: line 1: seq: command not found


I don't get it what's going on at all, so I really appreciate your help so far, thanks.

Ok, back to square one.

It looks like "seq" isn't part of the .sh scripting language, so maybe it was supposed to be /bash.

Can you send me the file and I can try?

Edit: Never mind, I just c/p the file from above.
 
Ok, this works and loops through the files, but doesn't seem to pull the correct data. Can you verify the line in the script where it points to a file?

Code:
#!/bin/bash
COUNTER=0
while [  $COUNTER -lt 449 ]; do
        wget http://xkcd.com/$i/
        wget `grep http://imgs.xkcd.com/comics/ index.html | head -1 | cut -d\" 
-f2`
        rm index.html
        let COUNTER=COUNTER+1
done

I basically got rid of the for loop and used a while loop... now its looping, but the actually get command is not working. Mess around with that and I will be back to help later.
 
Well the xkcd/$i/ one should run through xkcd.com/1/ to xkcd.com/449/, each of which is a webpage, but not specified as an html file.
 
This: (notice the variable change in the 4th line)

Code:
#!/bin/bash
COUNTER=0
while [  $COUNTER -lt 449 ]; do
        wget http://xkcd.com/$COUNTER/
        wget `grep http://imgs.xkcd.com/comics/ index.html | head -1 | cut -d\" -f2`
        rm index.html
        let COUNTER=COUNTER+1
done
 
Yeah, it sure is. Hmm.

I've found that all the comic files are linked to via http://imgs.xkcd.com/comics/
Is there any way of downloading them so that they retain the date of release? The order of them is kinda important :eek:

I can look into this further when I get home from work... I went home at lunch and ran those quick tests.

Send me a pm and we can email -- that will be easier.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.