##Code extractor using the StackExchange API The following implementation accesses the post via the API and extracts the code bits from the HTML and returns them as input cells. The function ``StackAPI`GetCode[]`` accepts the following input: ###1: Question/Answer ID You can call ``StackAPI`GetCode[id]``, where `id` is as in `http://mathematica.stackexchange.com/questions/id/question-title-here` or `http://mathematica.stackexchange.com/q/id/userid`. It also works for answers, which have a form similar to the latter URL. If you'd like to get the code from a question on a different site, you can supply an option as, for example, ``StackAPI`GetCode[id, Site -> "StackOverflow"]``. The default value of `Site` is `"Mathematica"` and possible options are the set of strings for which `site.stackexchange.com` works (note, I don't check if you're entering a valid site — it'll simply fail with some string parsing/replacing or generic box error) ###2: Shortform URL Alternately, you can directly enter the link to the respective question/answer (the one you grab by clicking on `link` under the post) and the site name and post id are automatically handled. For example, ``StackAPI`GetCode["http://meta.mathematica.stackexchange.com/a/307/5"]`` will fetch you the code for this answer. Unfortunately, you'll have to copy paste the following before you can do that :) --- Begin["StackAPI`"]; url[id_Integer,post_,site_]:= "http://api." <> site <> ".com/1.1/" <> post <> "/"<> ToString@id <> "?body=true"; url[link_String]:= Module[{id, post, site}, {site, post, id} = First@StringCases[link, "http://" ~~ x__ ~~ ".com/" ~~ y_ ~~ "/" ~~ z:DigitCharacter.. ~~ "/"... :> {x, y, z}] /. {"q" -> "questions", "a" -> "answers"}; {"http://api." <> site <> ".com/1.1/" <> post <> "/" <> id <> "?body=true", post} ]; body[id_Integer,post_,site_]:= OptionValue[Import[url[id,post,site],"JSON"], {post->"body"}]; body[link_String]:= OptionValue[Import[#,"JSON"], {#2->"body"}]& @@ url[link]; code = First@StringCases[#, ("<pre><code>" ~~ ("\n" ...) ~~ x__ ~~ ("\n" ...) ~~"</code></pre>") /; StringFreeQ[x, "<pre><code>" | "</code></pre>"] :> x ]&; convertEntities = StringReplace[#, {">" -> ">", "<" -> "<", "&" -> "&"}]&; codeCell = Scan[CellPrint@Cell[Defer@#, "Input"] &, #]&; GetCode[id_Integer, opts:OptionsPattern[{Site -> "Mathematica"}] ] := Module[{post, site}, post = "questions"; site = Switch[#, "StackOverflow", "stackoverflow", "SuperUser", "superuser", "ServerFault", "serverfault", __, ToLowerCase@# <> ".stackexchange"]&@OptionValue[Site]; code@Quiet@Check[ body[id, post, site], post = "answers";body[id,post, site], OptionValue::optnf ]//convertEntities//codeCell ]; GetCode[link_String]:=Module[{}, code@Quiet@body[link]//convertEntities//codeCell ]; End[]; Using ``StackAPI`GetCode[]`` will create a new `Input` cell and highlight it as per your current syntax highlighting scheme. **Note:** I don't do any rigorous error checking or check to see if you're entering a valid Stack Exchange URL or if the question/answer is deleted (deleted posts cannot be accessed via the API), etc. So if you get any errors, it might be worthwhile to check if there's something wrong on the site. Also, SE API limits you to 300 calls/day/IP, if I remember correctly. That's quite a lot of calls for any reasonable person and ideally, you shouldn't cross that. Nevertheless, a possibility of being throttled is something to keep in mind if you also happen to be playing with the API for other purposes such as site statistics, etc.