[Question]
  [
![WARNING: Answers may be useful to some code golfers.](https://i.stack.imgur.com/mT3Lt.png)
In many [image-processing](/questions/tagged/image-processing "show questions tagged 'image-processing'") challenges, the post contains images, which must be saved to a file in order to be able to work on the problem. This is an especially tedious manual task. We programmers should not have to be subjected to such drudgery. Your task is to automatically download all the images contained in a Code Golf.SE question.
## Rules
* Your program may connect to any part of `stackexchange.com`, but may not connect to any other domains, excepting the locations of the images (i.e., don't bother with a URL shortener).
* An integer *N* is given as input, on the command line or stdin.
* The URL `http://codegolf.stackexchange.com/questions/*N*` is guaranteed to be a valid link to a Code Golf question.
* Each image displayed in the body of question *N* must be saved to a file on the local computer. Either of the following locations is acceptable:
	+ The current directory
	+ A directory input by the user
* Your program must not save files other than the images in the question body (e.g. user avatars, or images contained in answers).
* Images must be saved with the same file extension as the original.
This is a [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") — write the shortest program you can.
**Validity criterion for answers**
There are various possible edge cases with multiple images of the same name, text with the same name as HTML elements, etc. An answer will be invalidated only if it can be shown to fail on some revision of a question posted before January 10, 2015.
  
[Answer]
# Mathematica, ~~211~~ 210 bytes
```
i=Import;FileNameTake@#~Export~i@#&/@ImportString["body"/.("items"/.i["http://api.stackexchange.com/2.2/questions/"<>InputString[]<>"?site=codegolf&filter=!*Lgp.gEWHA6BNP.l","JSON"])[[1]],{"HTML","ImageLinks"}]
```
Ungolfed:
```
i = Import;
FileNameTake@#~Export~i@# & /@ 
 ImportString[
  "body" /. (
    "items" /. 
      i["http://api.stackexchange.com/2.2/questions/" <> 
        InputString[] <> "?site=codegolf&filter=!*Lgp.gEWHA6BNP.l", 
       "JSON"]
  )[[1]], 
  {"HTML", "ImageLinks"}
 ]
```
It's pretty straightforward. I've set up a [filter](http://api.stackexchange.com/docs/questions-by-ids#order=desc&sort=activity&ids=44481&filter=!*Lgp.gEWHA6BNP.l&site=codegolf&run=true) for the StackExchange API, which returns only the body of a question. The code retrieves the question information with that filter and parses it as JSON. I select the correct element (the body), and use `ImportString` to parse the HTML and filter out all image URLs. `FileNameTake@#~Export~Import@#` then downloads each of the images and stores it in the current working directory with the same file name as that in the URL.
You can find out the current working directory with `Directory[]`.
In principle, there's a much shorter version, because `ImportString` can actually download all the files right away, instead of just giving me the URLs. But then I lose information about the original file type (since they are converted to `Image` objects upon download), so I can only save them all as the same type (PNG, say).
[Answer]
## Javascript - ~~149~~ 161 bytes
```
$.get("http://codegolf.stackexchange.com/q/"+prompt(),function(e){$(".post-text:first img",e).each(function(e,t){$('<a href="'+t.src+'"download>')[0].click()})})
```
with whitespace
```
$.get('http://codegolf.stackexchange.com/q/' + prompt(), function(d) {
  $('.post-text:first img',d).each(function(i,e){
   $('<a href="' + e.src + '"download>')[0].click();
  })
})
```
script has to be run from stackexchange site to work. ~~Will default to the current page if no question number is specified in the prompt~~
[Answer]
## Python 2 - 241 bytes
Pretty straightforward, can probably be golfed further. I search the site for all occurrences of `img src=` between the first occurrence of `post-text` and the `/div` immediately following that. Each image url is then read and saved to the working directory.
```
import string,sys,urllib,re;o=string.find;u=urllib.urlopen
r=u("http://codegolf.stackexchange.com/q/"+sys.argv[1]).read()
i=o(r,"post-text")
for p in re.findall(r'img src="([^"]*)',r[i:o(r,"/div",i)]):f=open(p[-9:],"wb");f.write(u(p).read())
```
[Answer]
# Mathematica, 195
```
x=XMLElement;c=Cases;i=Import;l=Infinity;FileNameTake@#~Export~i@#&/@(((c[#,x["img",{"src"->e_,_},___]:>e,l]&)@*(c[#,x[_,{__,"id"->"question",__},e_]:>e,l]&)@*(i[#,"XMLObject"] &))@InputString[])
```
This exports images in the same way that Martin did in his Mathematica solution, read his answer for more information about that. This approach is very different from his, instead of parsing the result from the API I parse the HTML page directly. Or rather, I parse the symbolic XML that Mathematica can generate from HTML.
[Answer]
# Python 2 - ~~398~~ ~~342~~ 334 bytes
The program download the SE page, extracts the post part (the post-text div element), finds urls that end in an image extension and downloads them. The images are saved as `img<n>.<ext>` in the current directory.
```
import urllib2 as u,re,sys
z=u.urlopen;i=1
p=z('http://codegolf.stackexchange.com/q/'+sys.argv[1]).read()
s=re.search(r'ss="po(.+?)/di',p,16).group(1)
for L in re.findall('"(h.+?://.*?)"',s):
 b=L.rsplit('.',1)
 if len(b)==2 and b[1].lower() in 'jpg jpeg png gif bmp'.split():
  open('img%u.%s'%(i,b[1]),'wb').write(z(L).read());i+=1
```
This program will also download images that are supplied as a link, not only embedded images. By giving each image a unique filename, name clashes are also avoided.
[Answer]
# Bash - 86 bytes
```
wget -r -l1 -np -Ajpg,jpeg,png,bmp,gif http://codegolf.stackexchange.com/questions/$1
```
Nothing wget won't fix.
`-np` prevents wget from entering upper directories(User Imgs)
`-A` only grabs files with the extension matching the list presented. `-r` is a recursive download. `-l` prevents wget from going too deep. `$1` is the question to grab.
[Answer]
# Node.js, ~~251~~ 247 Bytes
```
r=require,g=r('request'),g('http://codegolf.stackexchange.com/q/'+process.argv[2],function(_,_,b){r('cheerio').load(b)('#question .post-text img').each(function(i,a){s=a.attribs.src,g(s).pipe(r('fs').createWriteStream(i+r('path').basename(s)))})})
```
Uses `request` to make HTTP `GET`s and `cheerio` to parse the HTML. Name collisions are resolved by prepending the index of the current image to the basename of the file's URL. Images are saved to same directory as the current file.
[Answer]
# Lua, 200 bytes
```
r=require'socket.http'.request r('http://codegolf.stackexchange.com/questions/'.. ...):gsub('post.text(.-)div',function(p)p:gsub('src="(.-)"',function(i)io.open(i:sub(-9),'wb'):write((r(i)))end)end)
```
Accepts the number as a command-line argument.
Assumes any `src=` attribute will be for an `img` tag since these are the only tags with `src` attributes that stack exchange allows (right?).
Also note the `.. ...`. I'm particularly proud of that one.
]