Write a program that downloads itself

67

10

Write a program that connects to this site, downloads the very answer in which it is posted, extracts its own source code and prints it out. The output must be identical to the source code. Shortest code (in bytes) wins.

Rules:

  • No URL shorteners allowed.
  • The answer must have a regular format - a heading with the language name and size, optional description, code block, optional description and explanation. No unnatural delimiters allowed.
  • The output must originate from the actual code block posted on the site.
  • The functionality must not depend on the position in the answer list; it should work even if there are multiple pages and the answer it not on the first one.
  • New: special note for answers that are supposed to be run in a browser: it is ok to require running them on the codegolf domain (to obey the same-origin policy) but the domain and path should be included in the solution in order to make it fair.

aditsu quit because SE is EVIL

Posted 2014-05-17T18:07:49.517

Reputation: 22 326

39Catch-22: How am I supposed to test my submission? – Martin Ender – 2014-05-17T18:11:24.827

9I forsee people posting answers and deleting them, so they can test their code. – Justin – 2014-05-17T18:18:33.327

4@m.buettner answers can be tested on other answers (to other questions) first, then posted, then edited to change the URL :) – aditsu quit because SE is EVIL – 2014-05-17T18:20:24.297

I think the participants should agree on a code of conduct that no two answers will be in the same language with the same character count (i.e. be a gentleman and use an additional character if there's a tie), such that everyone can save characters but not having to scrape for author or ID of the post (so posts can be identified by the headline directly). ;) – Martin Ender – 2014-05-17T18:29:32.237

@m.buettner I'm not sure that would save characters... basically I can allow this technique, but I won't require such conduct – aditsu quit because SE is EVIL – 2014-05-17T18:36:19.237

1I bet the next question will be a program that program itself. – Fabricio – 2014-05-17T19:44:32.020

How does this challenge handle comments designed to interfere with an answer? – Reinstate Monica - ζ-- – 2014-05-17T23:14:46.210

8@hexafraction if comments are able to interfere with an answer, then the answer is not very good... – aditsu quit because SE is EVIL – 2014-05-17T23:41:28.857

Given enough answers one submission might end up on page 2, 3, ... Must the solutions account for this? – Sylwester – 2014-05-18T00:06:22.997

@Sylwester yes I already mentioned that in the question – aditsu quit because SE is EVIL – 2014-05-18T00:13:05.910

17A question stuck in my head: How to write a tweet that links to itself without using any URL shorteners, but by estimating the tweet id your tweet? – Ming-Tang – 2014-05-18T05:35:52.597

3@Sylwester that shouldn't be a problem - if you use the direct link to the answer (instead of the question), the correct page should be open already. – Martin Ender – 2014-05-18T17:55:13.843

Concerning your new rule for JavaScript because it is powerful on this question is just like create a rule "Only language used in production accepted" because GolfScript is not fair for the majority of code-golf questions. Anyway, this is just my opinion... – Michael M. – 2014-05-19T11:42:47.647

1@Michael The rule is just about the circumstances of running the code. It's like saying that a program that prints "/0/1/2/3/.../20" must not depend on being executed in a folder with that exact path. And if only production languages were accepted, somebody WILL use GolfScript in production :) – aditsu quit because SE is EVIL – 2014-05-19T13:03:13.663

is jQuery allowed? (and more broadly, is this commonly allowed/disallowed in this site?) – Martijn – 2014-05-20T09:26:33.190

@Martijn I see no reason to exclude it, as long as it follows the rules – aditsu quit because SE is EVIL – 2014-05-20T09:51:25.070

I proposed the opposite of this a bit ago: http://meta.codegolf.stackexchange.com/questions/1412/proposed-question-sandbox-mark-xii-retired/1528#1528 it never came to fruition.

– Cruncher – 2014-05-20T14:13:13.173

1

@m.buettner I love the way your catch-22 comment has exactly 22 upvotes :D Here: http://i.imgur.com/twJzcfa.png

– shortstheory – 2014-05-21T03:06:25.390

@SHiNKiROU That does not apply here, as you can post the answer and then edit it to include the url of the answer – Amith KK – 2014-05-21T08:52:00.870

Answers

34

Bash + coreutils + Lynx browser, 61 bytes

Thanks to @FDinoff for the tips:

lynx -dump codegolf.stackexchange.com/posts/28164/body|grep 2

Digital Trauma

Posted 2014-05-17T18:07:49.517

Reputation: 64 644

@DigitalTrauma Okay, you're right. I thought it was a typo since the Q no. is 28154. – ghosts_in_the_code – 2015-11-29T03:40:58.287

curl doesn't work? – NoOneIsHere – 2016-07-06T14:18:48.977

@NoOneIsHere I'm not aware that curl has an equivalent to lynx's -dump option, that just outputs the page content, and not the html tags – Digital Trauma – 2016-07-06T15:18:42.517

4And what happens if I type that magic word grep is looking for? – Shade – 2014-05-17T22:36:18.497

3lynx lynx lynx lynx. This comment will be grepped out (and the heading as well) – Reinstate Monica - ζ-- – 2014-05-17T22:38:04.917

1@hexafraction Awww. You had to go and ruin it! – Shade – 2014-05-17T22:40:37.860

I would assume that no answer above you can use a "keyword" so you could select only the first result. – Reinstate Monica - ζ-- – 2014-05-17T22:52:26.523

1Or he could just be more specific with something like grep lynx -d to match where the -dump flag is. Oh wait... Thats broken now too – Shade – 2014-05-17T23:06:19.703

codegolf.stackexchange.com/questions/28154/write-a-program-that-downloads-itself can be replaced with codegolf.stackexchange.com/q/28154. – Justin – 2014-05-18T00:39:43.763

8This url should work. codegolf.stackexchange.com/posts/28164/body And it ignores comments. I also think its within the rules that you can use it... – FDinoff – 2014-05-18T01:31:25.667

lynx -dump codegolf.stackexchange.com/posts/28164/body|tail -1 is 62 characters. (I think it should work if you edit your answer) – FDinoff – 2014-05-18T01:56:57.123

@FDinoff Thats great! Thanks! – Digital Trauma – 2014-05-18T04:03:44.133

2 2 2 2 2 2 does it still work? – haneefmubarak – 2014-05-18T13:23:35.487

@haneefmubarak Yep, still works :) Comments are not included in the changed URL format, as recommended by FDinoff. – Digital Trauma – 2014-05-18T14:01:36.223

3@DigitalTrauma awww... damn. – haneefmubarak – 2014-05-18T14:50:05.750

22

Ruby, 155 186 195 148 138 110 97 characters

require'open-uri';puts open('http://codegolf.stackexchange.com/posts/28159/body').read[/req.+;/];

I had to make it one line, because otherwise it would output newlines as \n instead of actual newlines.

  • +31 characters because I didn't notice some characters were being escaped.
  • +9 characters to get rid of the annoying backslash.
  • Thanks to Nathan Osman for saving 2 chars, and Ventero for saving 55 (!!!) by removing the need for most of the fixes listed above.

The explanation

Let's beautify this a bit first. However, I'm going to have to use a somewhat... interesting notation in this code. I can't use semicolons at all in this post, for reasons explained later, so I will use {SEMI} in place of semicolons instead.

require 'open-uri'
resp = open('http://codegolf.stackexchange.com/posts/28159/body').read
puts resp.match(/req.+{SEMI}/){SEMI}

Alright, now let's walk through this. The first two lines are fairly self-explanatory -- they fetch the HTML text of this answer.

Now, the last line is the interesting one here. You see that seemingly useless semicolon at the end of the code? It's absolutely required, and here's why.

First, resp.match extracts the code to be printed. The regexp it uses for this is the trick: /req.+{SEMI}/. It grabs the start of the code, REQuire'net/http', by searching for req (re would grab my REputation). Then, it finds the end of the code by searching for a semicolon! Since + is greedy by default, it will keep going until it finds the semicolon that signifies the end of the code. See why I can't use semicolons anymore?

After that, I don't have to unescape anything thanks to Ventero's fix of not using \ at all anymore. All I have to do is fix {AMPERSAND} changing into {AMPERSAND}amp{SEMI}, which can be achieved simply by removing the amp{SEMI} part. No need for this anymore because of new URL. After that, the original code has been retrieved! (Note: I can't use the ampersand either, because that gets HTML-encoded which causes a semicolon to be created.)

Doorknob

Posted 2014-05-17T18:07:49.517

Reputation: 68 138

I believe that the require'open-uri'; can be passed as a command line argument as -ropen-uri, which would save more bytes – Justin – 2015-08-24T06:11:18.807

Some characters are getting escaped.. – aditsu quit because SE is EVIL – 2014-05-17T19:55:12.357

1@aditsu Gah; didn't notice that. Fixed. – Doorknob – 2014-05-17T19:57:56.100

You're gonna hate this.. a backslash is getting duplicated. There's also a newline difference, but that's a minor thing. – aditsu quit because SE is EVIL – 2014-05-17T20:00:54.463

@aditsu Argh! :P Fixed also. The newline thing is because of puts; it could be fixed with print but meh. Just pretend there's a trailing newline in the code, even though SE won't be able to show it. – Doorknob – 2014-05-17T20:05:21.760

You can shave off two bytes by using withbody in the filter instead of !9WA((ItYa. – Nathan Osman – 2014-05-18T01:05:40.370

.body[/e>.+?</][2..-5] should save quite a few characters. – Ventero – 2014-05-18T04:13:57.490

And with something like .body[/req.+-3./], you can save even more, especially because then you can drop the CGI.unescapeHTML call (plus the require), and instead use .sub("amp;",""). Also, using require"open-uri" and open("http://api.stack....").read saves even more - though this requires Ruby >= 2, I think. To fix the newline, use $><< instead of puts (which saves another character). – Ventero – 2014-05-18T04:26:19.530

(With the .sub call, you'll have to change the RegExp to /req.+""./, if I'm not mistaken) – Ventero – 2014-05-18T04:36:03.947

@Ventero Wow! Thanks. I have to use net/http because otherwise un-gzipping the API's output costs too much. Can't use $><< because then I'd have to unescape < and >, which costs more than it saves. I also altered your regex a bit. – Doorknob – 2014-05-18T22:25:56.240

@Doorknob: When you use Ruby 2, you get uncompressed output from open-uri. But yeah, missed the part about unescaping <>. And I like the seemingly useless semicolon! :) – Ventero – 2014-05-18T22:28:41.057

@Ventero Hm. I'll look into that in a sec; I'm writing up an explanation right now. Thanks; I'm particularly proud of that semicolon trick because I find it extremely clever for some reason :P – Doorknob – 2014-05-18T22:32:11.243

With the new URL, you don't need the .sub anymore. And maybe you could use a similar trick with the semicolon at the beginning of your code (e.g. by prepending your code with another semicolon or an exclamation mark or something), which could save another character. – Ventero – 2014-05-18T22:57:51.130

1For the link, http://codegolf.stackexchange.com/a/28159 would give the same result as yours and would save some chars. – Mhmd – 2014-05-19T11:21:12.093

20

PowerShell - 69 62

(irm codegolf.stackexchange.com/posts/28236/body).div.pre.code

Rynant

Posted 2014-05-17T18:07:49.517

Reputation: 2 353

DOM in a shell. Nice! – fregante – 2014-05-19T20:49:12.120

Doesn't irm require Azure Rights Management? Without that module, I think you could do it with Invoke-WebRequest. – Scott Leadley – 2014-05-19T23:29:42.993

@ScottLeadley irm is the alias for Invoke-RestMethod and was introduced with PowerShell v3 core. http://www.computerperformance.co.uk/powershell/powershell3-alias.htm

– Rynant – 2014-05-19T23:48:22.167

10Holy crap. A PowerShell code golf answer with a length in the same order of magnitude as the leading answers. +1 – Adam Maras – 2014-05-20T06:01:11.160

@AdamMaras Ha, I know what you mean! It does happen occasionally though. http://codegolf.stackexchange.com/a/26811/4565 and http://codegolf.stackexchange.com/a/21982/4565 weren't too far from the lead.

– Rynant – 2014-05-20T13:31:16.547

@bfred.it: strictly speaking, this isn't DOM, which refers to a specific API rather than any HTML/XML APIs. – Lie Ryan – 2014-05-24T11:37:07.313

15

JavaScript - 123 122 101 95 92 91 87 86 114

with(new XMLHttpRequest)send(open(0,/\codegolf.stackexchange.com\posts\28175\body/,0)),alert(/w.*/.exec(response))

Runs in the console of your web browser on this page. Tested on the latest Chrome and Firefox.

edit: +28 bytes to add the full domain.

Firefox doesn't like my Regex URL trick anymore with this update :(

Here's the rule-breaking 86 byte solution:

with(new XMLHttpRequest)send(open(0,/posts\28175\body/,0)),alert(/w.*/.exec(response))

nderscore

Posted 2014-05-17T18:07:49.517

Reputation: 4 912

That made me awe. Multiple times. – fregante – 2014-05-19T02:46:38.573

1@bfred.it I just cut a byte using an interesting regex. I hope it makes you awe one more time. – nderscore – 2014-05-19T06:59:58.357

If printing to the console is an acceptable method of output, you can shorten by 7 chars by removing the alert. – Tejas Kale – 2014-05-19T12:29:34.127

Also , according to the new rule you gotta add codegolf.stackexchange.com/ to the url. – Tejas Kale – 2014-05-19T12:34:59.950

1@TejasKale From what I've seen people frown upon solutions that don't actually alert/document.write/console.log the answer. – nderscore – 2014-05-19T12:44:11.907

10

Ruby + wget + gunzip, 159 86 82 71

Using tip of @FDinoff to use http://codegolf.stackexchange.com/posts/28173/body.

puts `wget -qO- codegolf.stackexchange.com/posts/28173/body`[/pu.*\]/]

Tested. Thanks to @ace and @Bob for command line optimization.

dtldarek

Posted 2014-05-17T18:07:49.517

Reputation: 321

2You can combine the flags in wget, as in wget -qO- url. Also, in bash you do not need the double quotes for the url, so this may also work for you. – user12205 – 2014-05-18T10:46:01.807

You can leave out the http://. – Bob – 2014-05-18T14:44:35.570

6

CJam - 53

"codegolf.stackexchange.com/posts/28184/body"g54/1=);

I'm making this community wiki since I'm answering my own question and don't really want to compete :p
Credits to FDinoff for the URL choice.

aditsu quit because SE is EVIL

Posted 2014-05-17T18:07:49.517

Reputation: 22 326

1@Cruncher ); doesn't look too smiley to me... – MD XF – 2017-05-19T22:50:10.383

Woot, +1 for smiley face in code – Cruncher – 2014-05-20T14:18:55.490

5

JavaScript, 228

r=new XMLHttpRequest()
c='code'
r.open('GET','//'+c+'golf.stackexchange.com/posts/28157/body')
r.onreadystatechange=function(){this.readyState==4&&alert((a=r.responseText).substr(i=a.indexOf(c)+5,a.indexOf('/'+c)-i-1))}
r.send()

Runs on this page.

user12205

Posted 2014-05-17T18:07:49.517

Reputation: 8 752

How do you run it? – aditsu quit because SE is EVIL – 2014-05-17T19:05:05.703

@aditsu It is supposed to be run on the JavaScript console of a browser. But I am still testing (and fixing) it, please wait – user12205 – 2014-05-17T19:09:32.250

@aditsu It should work now. Open your browser console (press F12) and paste this code there. – user12205 – 2014-05-17T19:27:54.630

you sir, need a if(this.readyState == this.DONE) inside the function. – Fabricio – 2014-05-17T20:07:15.383

@Fabricio You're right! Thanks a lot :) – user12205 – 2014-05-17T21:01:38.420

You can save 10 chars, if you ommit the parens from the XMLHttpRequest and use with instead of assigning the XHR to r. with(new XMLHttpRequest){open('GET','/posts/28157/body');onreadystatechange=function(){if(readyState==4)alert((a=responseText).substr(i=a.indexOf(c='code')+5,a.indexOf('/'+c)-i-1))};send()} – C5H8NNaO4 – 2014-05-20T07:55:59.750

@C5H8NNaO4 I know I can, but I'd rather not, otherwise people may think I am copying the other JS answer. (And in fact I got an un-upvote after copying the URL...) – user12205 – 2014-05-20T09:44:09.647

1@ace I see :) I didn't saw the other js answer until now. Then take this upvote from me – C5H8NNaO4 – 2014-05-20T09:49:59.717

Nice answer. How do you get the URL of your post? – bacchusbeale – 2014-05-21T19:49:16.640

@bacchusbeale By clicking the "share" button below the answer, you can see the post number 28157. As for the format of the URL (/posts/[number]/body), I copied it from other's answer... Originally I used the StackExchange API. – user12205 – 2014-05-21T20:53:51.403

5

Java now 634, 852, was 1004

Code has been updated; thanks for suggestions. Golfed: now replaces &gt with >

//bacchus
package golf;
import java.net.*;
import java.util.*;
public class G{
public static void main(String[] a) throws Exception {
Scanner z;
URL u;
int x=0;
String s;
u=new URL("http://codegolf.stackexchange.com/questions/28154/write-a-program-that-downloads-itself");
z=new Scanner(u.openConnection().getInputStream());
z.useDelimiter("\\s*//bacchus\\s*");
while(z.hasNext())
{
s=z.next();
s=s.replace("&gt;", ">");
if(x>0)System.out.println("//bacchus\n"+s);
x++;
if(x>2)break;
}
System.out.println("//bacchus\n");
}
}
//bacchus

Submitting for testing, I will edit and try golfing it shortly. Needed to change x>1 to x>2 because test string is also in my code. Note: Code golf replaces > symbol to &gt.

//bacchus
package golf;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class Golf {

    public static void main(String[] args) throws IOException {
        URL u;
        URLConnection c;
        InputStream i;
        InputStreamReader r;
        BufferedReader b;
        String s;
        int x=0;
        try {
            u=new URL("http://codegolf.stackexchange.com/questions/28154/write-a-program-that-downloads-itself");
            c=u.openConnection();
            i=c.getInputStream();
            r=new InputStreamReader(i);
            b=new BufferedReader(r);
            while((s=b.readLine())!=null)
            {
                if(s.contains("//bacchus")) x++;
                if(x>0)System.out.println(s);
                if(x>2) break;
            }
            i.close();
            b.close();
        } catch (MalformedURLException ex) {

        }
    }

}
//bacchus

bacchusbeale

Posted 2014-05-17T18:07:49.517

Reputation: 1 235

7How do you handle comments containing //bacchus? – Reinstate Monica - ζ-- – 2014-05-17T23:51:59.543

3You could inline a lot of stuff, try-with-resources, and use * imports to save a lot of code. – Simon Kuang – 2014-05-18T07:23:56.357

@SimonKuang - I'd also just leave the streams open rather than closing stuff. Also, throws Exception rather than trying to handle anything. Also, I think using a Scanner rather than a BufferedReader would be simpler, especially as you could set the delimiter to //bacchus, which would make things somewhat easier... – Jules – 2014-05-18T16:00:45.660

5

Python, 175 167 bytes

This uses two external libraries; I didn't read that it was unauthorized.

import bs4,requests
print(bs4.BeautifulSoup(requests.get('http://codegolf.stackexchange.com/q/28154').text).select('#answer-28171')[0].select('pre > code')[0].string)

Longer, but nicer looking code:

import bs4, requests
request = requests.get('http://codegolf.stackexchange.com/q/28154')
soup = bs4.BeautifulSoup(request.text)
answer = soup.select('#answer-28171')[0]
code = answer.select('pre > code')[1].string
print(code)

MeldSnake

Posted 2014-05-17T18:07:49.517

Reputation: 51

1The questions in the url can be replaced with q: http://codegolf.stackexchange.com/q/28154 – Justin – 2014-05-18T00:37:05.743

1The space in bs4, requests (line 1) can be removed to reduce 1 byte. – user12205 – 2014-05-18T10:48:18.453

5

Rebmu, 91 characters

Due to the Catch-22 I have to post to get this answer's URL. :-/ Okay, got it.

paTSrd http://codegolf.stackexchange.com/a/28154[th<a name="28154">th<code>cpCto</code>]prC

Rebmu is a dialect of Rebol, and you can read all 'bout it. The equivalent Rebol here would be:

parse to-string read http://codegolf.stackexchange.com/a/28154 [
    thru <a name="28154">
    thru <code>
    copy c to </code>
]
print c

Rebol's PARSE is a sort of highly-literate answer to RegEx. It starts a parser position of the input (which can be any series, including structural blocks...binary data...or string types). The rules are a language for how the parse position moves.

Tags and URLs are really just strings under the hood in the language. But they are "flavored", and as Rebol is dynamically typed you can check that type. READ for instance knows that if you give it a URL-flavored string, then it should dispatch to a scheme handler to do the reading. (In this case, the one registered for HTTP). You get back UTF-8 bytes by default, so we use to-string to decode that and get a series of codepoints in a normal Unicode string.

In the case of the parse dialect, encountering a tag type is just matched as if it were a string that looked like the tag. THRU is an instruction meaning "skip until the ensuing rule is matched, and then place the match position at the end of what you just matched." (TO is the analogue that matches, but leaves the parse position before the element).

So we zip along past the <a name="28154">. Then we zip past the next occurrence of <code>, with our parse position now located right after the >. PARSE's COPY command then lets us copy data up to another rule, in this case that rule is [TO </code>]... so we get into the variable C everything up until right before that <.

Cool, huh? :-)

Technically I could shave more off it, for instance by seeking TO "</" and that saves three characters--there's no need to match the whole </code> end tag when just </ would do. Similar arguments could me made for the start tag. But Rebmu is about literate golfing...even if you might think it looks odd at first!

UPDATE: the /body trick is out of the bag, but I'm similarly going to leave it as-is...because I think it is more educational this way.

HostileFork says dont trust SE

Posted 2014-05-17T18:07:49.517

Reputation: 2 292

4

Haskell, 563 613 bytes

import Control.Monad
import Data.List
import Network.HTTP
m%f=join(fmap f m)
q s=(simpleHTTP(getRequest"http://codegolf.stackexchange.com/questions/28154/write-a-program-that-downloads-itself?answertab=oldest#tab-top"))%getResponseBody%(putStrLn.head.filter((==)(s++show s)).map(take 613).tails)
main=q"import Control.Monad\nimport Data.List\nimport Network.HTTP\nm%f=join(fmap f m)\nq s=(simpleHTTP(getRequest\"http://codegolf.stackexchange.com/questions/28154/write-a-program-that-downloads-itself?answertab=oldest#tab-top\"))%getResponseBody%(putStrLn.head.filter((==)(s++show s)).map(take 613).tails)\nmain=q"

Tested. Has page support via "oldest posts" feature. Uses quine-line structure to find what to print. The import Control.Monad is only because >>= generates &gt; in HTML.

dtldarek

Posted 2014-05-17T18:07:49.517

Reputation: 321

4

Javascript +jQuery, 87, 67

I'm not sure wether I'm allowed to use jQuery, but:

$('body').load('//codegolf.stackexchange.com/posts/28268/body pre')

Javascript + jQuery, if excecuted in this page: 27, 25

For fun, if it would be excecuted here:

$('[id$=268] pre').html()

$('[id$=28268] pre').html()

Martijn

Posted 2014-05-17T18:07:49.517

Reputation: 713

1This outputs more than the source code. – nderscore – 2014-05-20T16:05:09.090

167: $('body').load('//codegolf.stackexchange.com/posts/28268/body pre') – nderscore – 2014-05-20T16:07:29.133

You are correct, I wrongly asumed the whole reply instead of the code – Martijn – 2014-05-20T19:09:30.493

4

w3m 45 characters

w3m codegolf.stackexchange.com/a/28336|grep ☻

gnibbler

Posted 2014-05-17T18:07:49.517

Reputation: 14 170

1☺. Does it still work? Edit: Looks like it does. – user12205 – 2014-05-21T12:04:38.270

@ace I get your comment when I run it – FDinoff – 2014-05-22T02:26:33.927

Fixed :) Anyone else? – gnibbler – 2014-05-22T02:42:06.680

3

Dart, 164

I thought I'd try this in Dart, is pretty fun to use imo.

This can be run in the console in DartEditor, but does require the http package added in pubspec.yaml

import"package:http/http.dart"as h;h.read("http://codegolf.stackexchange.com/posts/28215/body").then((s){print(new RegExp(r"im.+(?:})").firstMatch(s).group(0));});}

Ungolfed version:

import "package:http/http.dart" as h;

void main()
{
  h.read("http://codegolf.stackexchange.com/posts/28215/body").then((s)
  {
    print(new RegExp(r"im.+(?:})").firstMatch(s).group(0));
  });
}

MisterBla

Posted 2014-05-17T18:07:49.517

Reputation: 181

2

R 114 characters

library(XML);cat(xpathSApply(xmlParse("http://codegolf.stackexchange.com/posts/28216/body"),'//code',xmlValue)[1])

No real magic here: it takes the value of the field between the html tags <code></code>. Uses library XML (as one can see in the code quite obviously). Outputs the result as stdout.

plannapus

Posted 2014-05-17T18:07:49.517

Reputation: 8 610

1

bash + awk, 71 bytes

curl -sL codegolf.stackexchange.com/q/28154 |awk -F\> '/\#/ {print $3}'

user155406

Posted 2014-05-17T18:07:49.517

Reputation: 125

2Doesn't seem to work - it outputs a lot of other stuff along with this answer. – Riking – 2014-05-18T09:53:39.253

@Riking true, it seems to be position-dependent (breaking the last rule) – aditsu quit because SE is EVIL – 2014-05-18T10:01:46.330

You can leave out the http://. – Bob – 2014-05-18T14:45:28.197

@user155406: note that each answer has a URL - this one is http://codegolf.stackexchange.com/a/28179/14710

– Phil H – 2014-05-19T15:26:17.280

1

Ruby, 237 215 146 132

require'mechanize'
a=Mechanize.new
puts a.get('http://codegolf.stackexchange.com/a/28159').search('.lang-rb code:nth-child(1)').text

Mhmd

Posted 2014-05-17T18:07:49.517

Reputation: 2 019

Pretty sure you can remove a few spaces here and there to save a few bytes. – MisterBla – 2014-05-19T10:48:28.270

@richard who cares I'm not going to win anyway. – Mhmd – 2014-05-19T10:52:18.170

1Do it for the lols, not for winning. – MisterBla – 2014-05-19T10:55:43.483

@RichardA done, and I also removed few chars from the regexp. – Mhmd – 2014-05-19T11:29:33.737

1

Java, 300 294

import java.net.*;import java.util.*;public class G{public static void main (String [] a) throws Exception{Scanner s=new Scanner(new URL("http://codegolf.stackexchange.com/posts/28189/body").openConnection().getInputStream()).useDelimiter("./?[c]ode\\W");s.next();System.out.print(s.next());}}

An improved version of bacchusbeale's answer which:

  • doesn't close resources unnecessarily
  • doesn't declare unnecessary variables
  • uses a Scanner to avoid having to loop over the input
  • uses a regexp that doesn't match itself to avoid having to skip over a middle occurrence of the start/end marker.

Updated:

  • Use a direct URL to the post, so we don't need a unique comment to identify the start/end of the code; now uses <code>[...]</code> as the delimiters to search for (actually using the regular expression "./?[c]ode\W", so as to avoid having to decode &lt; and &gt; -- the "\W" is necessary rather than the shorter "." to avoid it matching part of the URL to the post, unfortunately, which costs 2 characters, and the square brackets around c prevent the regex matching itself).

Jules

Posted 2014-05-17T18:07:49.517

Reputation: 211

1You have a bunch of unnecessary spaces. Also your class doesn't need to be public. – aditsu quit because SE is EVIL – 2014-05-19T08:54:11.827

1openConnection().getInputStream() can also be shortened to openStream() – aditsu quit because SE is EVIL – 2014-05-19T09:00:02.020

1

w3m 55 bytes

w3m codegolf.stackexchange.com/posts/28242/body|grep x

Based on @DigitalTrauma

Moop

Posted 2014-05-17T18:07:49.517

Reputation: 723

1

Processing, 90

print(loadStrings("http://codegolf.stackexchange.com/posts/28657/body")[2].substring(11));

Edit: Finally got it!

segfaultd

Posted 2014-05-17T18:07:49.517

Reputation: 1 189

0

Javascript, 138

a=window.open("http://codegolf.stackexchange.com/posts/28160/body");setTimeout('alert(a.document.body.innerHTML.match(/a=.*9\\)/)[0])',99)

This works assuming that the page loads in under 99 ms. It also has to be run via a console opened on a codegolf.SE page, because of the same origin policy.

Zaq

Posted 2014-05-17T18:07:49.517

Reputation: 1 525

Just a note: you don't need the slug in the URL, and questions can be replaced by q. – Schism – 2014-05-17T21:59:43.453

1Note that you could do http://codegolf.stackexchange.com/a/28160 instead of http://codegolf.stackexchange.com/a/28160/12551 – Justin – 2014-05-18T00:36:24.537

Chrome doesn't like this: "Uncaught TypeError: Cannot read property 'document' of undefined" – Spedwards – 2014-05-19T07:00:07.837

@Spedwards you should disable the popup blocker. – nderscore – 2014-05-19T15:55:56.263

0

Perl 5.10, 155 127 122 117 bytes

use XML::LibXML;say XML::LibXML->new->parse_file('http://codegolf.stackexchange.com/posts/28330/body')->find('//pre')

Using XML::LibXML.

nwellnhof

Posted 2014-05-17T18:07:49.517

Reputation: 10 037

0

Shell and xmllint, 82 bytes

xmllint --xpath 'string(//pre)' http://codegolf.stackexchange.com/posts/28333/body

nwellnhof

Posted 2014-05-17T18:07:49.517

Reputation: 10 037

0

Python, 164

Works by extracting the text between the code tags. It is quite long but it will always function correctly unless the html page is edited directly or a new code block is added before the one below (having a block of code after should have no effect on the output of the program).

import urllib2
print urllib2.urlopen("http://codegolf.stackexchange.com/posts/28617/body").read().split(chr(60)+"code"+chr(62))[1].split(chr(60)+"/code"+chr(62))[0]

icedvariables

Posted 2014-05-17T18:07:49.517

Reputation: 453