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.
-
42\$\begingroup\$ Catch-22: How am I supposed to test my submission? \$\endgroup\$Martin Ender– Martin Ender2014年05月17日 18:11:24 +00:00Commented May 17, 2014 at 18:11
-
10\$\begingroup\$ I forsee people posting answers and deleting them, so they can test their code. \$\endgroup\$Justin– Justin2014年05月17日 18:18:33 +00:00Commented May 17, 2014 at 18:18
-
4\$\begingroup\$ @m.buettner answers can be tested on other answers (to other questions) first, then posted, then edited to change the URL :) \$\endgroup\$aditsu quit because SE is EVIL– aditsu quit because SE is EVIL2014年05月17日 18:20:24 +00:00Commented May 17, 2014 at 18:20
-
8\$\begingroup\$ @hexafraction if comments are able to interfere with an answer, then the answer is not very good... \$\endgroup\$aditsu quit because SE is EVIL– aditsu quit because SE is EVIL2014年05月17日 23:41:28 +00:00Commented May 17, 2014 at 23:41
-
17\$\begingroup\$ A 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? \$\endgroup\$Ming-Tang– Ming-Tang2014年05月18日 05:35:52 +00:00Commented May 18, 2014 at 5:35
27 Answers 27
Bash + coreutils + Lynx browser, 61 bytes
Thanks to @FDinoff for the tips:
lynx -dump codegolf.stackexchange.com/posts/28164/body|grep 2
-
4\$\begingroup\$ And what happens if I type that magic word grep is looking for? \$\endgroup\$Shade– Shade2014年05月17日 22:36:18 +00:00Commented May 17, 2014 at 22:36
-
3\$\begingroup\$ lynx lynx lynx lynx. This comment will be grepped out (and the heading as well) \$\endgroup\$nanofarad– nanofarad2014年05月17日 22:38:04 +00:00Commented May 17, 2014 at 22:38
-
1\$\begingroup\$ @hexafraction Awww. You had to go and ruin it! \$\endgroup\$Shade– Shade2014年05月17日 22:40:37 +00:00Commented May 17, 2014 at 22:40
-
9\$\begingroup\$ This 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... \$\endgroup\$FDinoff– FDinoff2014年05月18日 01:31:25 +00:00Commented May 18, 2014 at 1:31 -
3\$\begingroup\$ @DigitalTrauma awww... damn. \$\endgroup\$haneefmubarak– haneefmubarak2014年05月18日 14:50:05 +00:00Commented May 18, 2014 at 14:50
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 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.)\
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. (削除ここまで)
-
\$\begingroup\$ Some characters are getting escaped.. \$\endgroup\$aditsu quit because SE is EVIL– aditsu quit because SE is EVIL2014年05月17日 19:55:12 +00:00Commented May 17, 2014 at 19:55
-
1\$\begingroup\$ @aditsu Gah; didn't notice that. Fixed. \$\endgroup\$Doorknob– Doorknob2014年05月17日 19:57:56 +00:00Commented May 17, 2014 at 19:57
-
\$\begingroup\$ You're gonna hate this.. a backslash is getting duplicated. There's also a newline difference, but that's a minor thing. \$\endgroup\$aditsu quit because SE is EVIL– aditsu quit because SE is EVIL2014年05月17日 20:00:54 +00:00Commented May 17, 2014 at 20:00
-
\$\begingroup\$ @aditsu Argh! :P Fixed also. The newline thing is because of
puts
; it could be fixed withprint
but meh. Just pretend there's a trailing newline in the code, even though SE won't be able to show it. \$\endgroup\$Doorknob– Doorknob2014年05月17日 20:05:21 +00:00Commented May 17, 2014 at 20:05 -
1\$\begingroup\$ For the link,
http://codegolf.stackexchange.com/a/28159
would give the same result as yours and would save some chars. \$\endgroup\$Mohammad– Mohammad2014年05月19日 11:21:12 +00:00Commented May 19, 2014 at 11:21
PowerShell - (削除) 69 (削除ここまで) 62
(irm codegolf.stackexchange.com/posts/28236/body).div.pre.code
-
\$\begingroup\$ DOM in a shell. Nice! \$\endgroup\$fregante– fregante2014年05月19日 20:49:12 +00:00Commented May 19, 2014 at 20:49
-
\$\begingroup\$ Doesn't irm require Azure Rights Management? Without that module, I think you could do it with Invoke-WebRequest. \$\endgroup\$Scott Leadley– Scott Leadley2014年05月19日 23:29:42 +00:00Commented May 19, 2014 at 23:29
-
\$\begingroup\$ @ScottLeadley
irm
is the alias forInvoke-RestMethod
and was introduced with PowerShell v3 core. computerperformance.co.uk/powershell/powershell3-alias.htm \$\endgroup\$Rynant– Rynant2014年05月19日 23:48:22 +00:00Commented May 19, 2014 at 23:48 -
10\$\begingroup\$ Holy crap. A PowerShell code golf answer with a length in the same order of magnitude as the leading answers. +1 \$\endgroup\$Adam Maras– Adam Maras2014年05月20日 06:01:11 +00:00Commented May 20, 2014 at 6:01
-
\$\begingroup\$ @AdamMaras Ha, I know what you mean! It does happen occasionally though. codegolf.stackexchange.com/a/26811/4565 and codegolf.stackexchange.com/a/21982/4565 weren't too far from the lead. \$\endgroup\$Rynant– Rynant2014年05月20日 13:31:16 +00:00Commented May 20, 2014 at 13:31
JavaScript - (削除) 123 (削除ここまで) (削除) 122 (削除ここまで) (削除) 101 (削除ここまで) (削除) 95 (削除ここまで) (削除) 92 (削除ここまで) (削除) 91 (削除ここまで) (削除) 87 (削除ここまで) (削除) 86 (削除ここまで) 114
with(new XMLHttpRequest)send(open(0,/\codegolf.stackexchange.com\posts28175円\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,/posts28175円\body/,0)),alert(/w.*/.exec(response))
-
\$\begingroup\$ That made me awe. Multiple times. \$\endgroup\$fregante– fregante2014年05月19日 02:46:38 +00:00Commented May 19, 2014 at 2:46
-
1\$\begingroup\$ @bfred.it I just cut a byte using an interesting regex. I hope it makes you awe one more time. \$\endgroup\$nderscore– nderscore2014年05月19日 06:59:58 +00:00Commented May 19, 2014 at 6:59
-
\$\begingroup\$ If printing to the console is an acceptable method of output, you can shorten by 7 chars by removing the alert. \$\endgroup\$Tejas Kale– Tejas Kale2014年05月19日 12:29:34 +00:00Commented May 19, 2014 at 12:29
-
\$\begingroup\$ Also , according to the new rule you gotta add
codegolf.stackexchange.com/
to the url. \$\endgroup\$Tejas Kale– Tejas Kale2014年05月19日 12:34:59 +00:00Commented May 19, 2014 at 12:34 -
1\$\begingroup\$ @TejasKale From what I've seen people frown upon solutions that don't actually alert/document.write/console.log the answer. \$\endgroup\$nderscore– nderscore2014年05月19日 12:44:11 +00:00Commented May 19, 2014 at 12:44
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.
-
2\$\begingroup\$ You can combine the flags in
wget
, as inwget -qO- url
. Also, in bash you do not need the double quotes for the url, so this may also work for you. \$\endgroup\$user12205– user122052014年05月18日 10:46:01 +00:00Commented May 18, 2014 at 10:46 -
\$\begingroup\$ You can leave out the
http://
. \$\endgroup\$Bob– Bob2014年05月18日 14:44:35 +00:00Commented May 18, 2014 at 14:44
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.
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.
Java now 634, 852, was 1004
Code has been updated; thanks for suggestions. Golfed: now replaces > 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(">", ">");
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 >.
//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
-
7\$\begingroup\$ How do you handle comments containing
//bacchus
? \$\endgroup\$nanofarad– nanofarad2014年05月17日 23:51:59 +00:00Commented May 17, 2014 at 23:51 -
3\$\begingroup\$ You could inline a lot of stuff, try-with-resources, and use
*
imports to save a lot of code. \$\endgroup\$Simon Kuang– Simon Kuang2014年05月18日 07:23:56 +00:00Commented May 18, 2014 at 7:23 -
\$\begingroup\$ @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... \$\endgroup\$Jules– Jules2014年05月18日 16:00:45 +00:00Commented May 18, 2014 at 16:00
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)
-
1\$\begingroup\$ The
questions
in the url can be replaced withq
:http://codegolf.stackexchange.com/q/28154
\$\endgroup\$Justin– Justin2014年05月18日 00:37:05 +00:00Commented May 18, 2014 at 0:37 -
1\$\begingroup\$ The space in
bs4, requests
(line 1) can be removed to reduce 1 byte. \$\endgroup\$user12205– user122052014年05月18日 10:48:18 +00:00Commented May 18, 2014 at 10:48
w3m 45 characters
w3m codegolf.stackexchange.com/a/28336|grep ☻
-
1\$\begingroup\$ ☺. Does it still work? Edit: Looks like it does. \$\endgroup\$user12205– user122052014年05月21日 12:04:38 +00:00Commented May 21, 2014 at 12:04
-
\$\begingroup\$ @ace I get your comment when I run it \$\endgroup\$FDinoff– FDinoff2014年05月22日 02:26:33 +00:00Commented May 22, 2014 at 2:26
-
\$\begingroup\$ Fixed :) Anyone else? 😁 \$\endgroup\$gnibbler– gnibbler2014年05月22日 02:42:06 +00:00Commented May 22, 2014 at 2:42
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.
-
\$\begingroup\$ How do you run it? \$\endgroup\$aditsu quit because SE is EVIL– aditsu quit because SE is EVIL2014年05月17日 19:05:05 +00:00Commented May 17, 2014 at 19:05
-
\$\begingroup\$ @aditsu It is supposed to be run on the JavaScript console of a browser. But I am still testing (and fixing) it, please wait \$\endgroup\$user12205– user122052014年05月17日 19:09:32 +00:00Commented May 17, 2014 at 19:09
-
\$\begingroup\$ @aditsu It should work now. Open your browser console (press F12) and paste this code there. \$\endgroup\$user12205– user122052014年05月17日 19:27:54 +00:00Commented May 17, 2014 at 19:27
-
\$\begingroup\$ you sir, need a
if(this.readyState == this.DONE)
inside the function. \$\endgroup\$Fabricio– Fabricio2014年05月17日 20:07:15 +00:00Commented May 17, 2014 at 20:07 -
1\$\begingroup\$ @ace I see :) I didn't saw the other js answer until now. Then take this upvote from me \$\endgroup\$C5H8NNaO4– C5H8NNaO42014年05月20日 09:49:59 +00:00Commented May 20, 2014 at 9:49
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 >
in HTML.
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()
(削除ここまで)
-
1\$\begingroup\$ This outputs more than the source code. \$\endgroup\$nderscore– nderscore2014年05月20日 16:05:09 +00:00Commented May 20, 2014 at 16:05
-
1\$\begingroup\$ 67:
$('body').load('//codegolf.stackexchange.com/posts/28268/body pre')
\$\endgroup\$nderscore– nderscore2014年05月20日 16:07:29 +00:00Commented May 20, 2014 at 16:07 -
\$\begingroup\$ You are correct, I wrongly asumed the whole reply instead of the code \$\endgroup\$Martijn– Martijn2014年05月20日 19:09:30 +00:00Commented May 20, 2014 at 19:09
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));
});
}
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.
APL (Dyalog Unicode), 108 bytes
6 3⌷⎕XML{(⎕SE.SALT.New'HttpCommand'('GET'⍵)).Run.Data}'https://codegolf.stackexchange.com/posts/211205/body'
Don't Try it online, try it locally
(削除) Correct link coming soon. (削除ここまで)
Output:
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<
and>
-- 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).
-
1\$\begingroup\$ You have a bunch of unnecessary spaces. Also your class doesn't need to be public. \$\endgroup\$aditsu quit because SE is EVIL– aditsu quit because SE is EVIL2014年05月19日 08:54:11 +00:00Commented May 19, 2014 at 8:54
-
1\$\begingroup\$ openConnection().getInputStream() can also be shortened to openStream() \$\endgroup\$aditsu quit because SE is EVIL– aditsu quit because SE is EVIL2014年05月19日 09:00:02 +00:00Commented May 19, 2014 at 9:00
w3m 55 bytes
w3m codegolf.stackexchange.com/posts/28242/body|grep x
Based on @DigitalTrauma
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
-
\$\begingroup\$ Pretty sure you can remove a few spaces here and there to save a few bytes. \$\endgroup\$MisterBla– MisterBla2014年05月19日 10:48:28 +00:00Commented May 19, 2014 at 10:48
-
\$\begingroup\$ @richard who cares I'm not going to win anyway. \$\endgroup\$Mohammad– Mohammad2014年05月19日 10:52:18 +00:00Commented May 19, 2014 at 10:52
-
1\$\begingroup\$ Do it for the lols, not for winning. \$\endgroup\$MisterBla– MisterBla2014年05月19日 10:55:43 +00:00Commented May 19, 2014 at 10:55
-
\$\begingroup\$ @RichardA done, and I also removed few chars from the regexp. \$\endgroup\$Mohammad– Mohammad2014年05月19日 11:29:33 +00:00Commented May 19, 2014 at 11:29
Processing, 90
print(loadStrings("http://codegolf.stackexchange.com/posts/28657/body")[2].substring(11));
Edit: Finally got it!
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]
-
\$\begingroup\$ Very late but I appreciate the smart use of
chr()
to avoid the code containing any characters that need escaping. \$\endgroup\$mousetail– mousetail2022年10月04日 08:19:10 +00:00Commented Oct 4, 2022 at 8:19
bash + awk, 71 bytes
curl -sL codegolf.stackexchange.com/q/28154 |awk -F\> '/\#/ {print 3ドル}'
-
2\$\begingroup\$ Doesn't seem to work - it outputs a lot of other stuff along with this answer. \$\endgroup\$VerdantOzark– VerdantOzark2014年05月18日 09:53:39 +00:00Commented May 18, 2014 at 9:53
-
\$\begingroup\$ @Riking true, it seems to be position-dependent (breaking the last rule) \$\endgroup\$aditsu quit because SE is EVIL– aditsu quit because SE is EVIL2014年05月18日 10:01:46 +00:00Commented May 18, 2014 at 10:01
-
\$\begingroup\$ You can leave out the
http://
. \$\endgroup\$Bob– Bob2014年05月18日 14:45:28 +00:00Commented May 18, 2014 at 14:45 -
\$\begingroup\$ @user155406: note that each answer has a URL - this one is codegolf.stackexchange.com/a/28179/14710 \$\endgroup\$Phil H– Phil H2014年05月19日 15:26:17 +00:00Commented May 19, 2014 at 15:26
05AB1E, 50 bytes
žY’ƒËŠˆ.‚‹o.ŒŒ/„¤/’•4m6•’/†Î’J.w4°.W'ƒË¡1è¦6F ̈}.V
Screenshot of a local run:
(CMD doesn't support the 05AB1E codepage, hence the weird output for non-ASCII characters.)
Explanation:
žY # Push constant "https://"
’ƒËŠˆ.‚‹o.ŒŒ/„¤/’ # Push dictionary string "codegolf.stackexchange.com/posts/"
•4m6• # Push compressed integer 272346
’/†Î’ # Push dictionary string "/body"
J # Join all strings on the stack together:
# "https://codegolf.stackexchange.com/posts/272346/body"
.w # Go to this url, and push its HTML contents
4°.W #†Sleep for 10 seconds (10**4 ms)
'ƒË¡ '# Split it on dictionary word "code"
1è # Pop and leave the second item (0-based 1st)
¦ # Remove its leading ">"
6F # Loop 6 times:
̈ # Remove its trailing character ("}.V\n/>")
}.V # After the loop: evaluate this string as 05AB1E code
# (after which the result is output implicitly)
See this 05AB1E tip of mine (sections How to use the dictionary? and How to compress large integers?) to understand why ’ƒËŠˆ.‚‹o.ŒŒ/„¤/’
is "codegolf.stackexchange.com/posts/"
; ’/†Î’
is "/body"
; 'ƒË
is "code"
; and •4m6•
is 272346
.
† The sleep is apparently necessary, since otherwise Stackexchange itself will give the error: You may only load 1 post body every second
.
Note: I've used 6
instead of 3
to also remove the trailing .V
(and optional }
), otherwise it would go into an infinite loop of getting its HTML contents without ever printing anything.. If getting into an infinite loop was the intend of this challenge, the trailing 6F ̈}.V
could be ̈ ̈ ̈.V
(-1 byte) instead.
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.
-
\$\begingroup\$ Just a note: you don't need the slug in the URL, and questions can be replaced by q. \$\endgroup\$Schism– Schism2014年05月17日 21:59:43 +00:00Commented May 17, 2014 at 21:59
-
1\$\begingroup\$ Note that you could do
http://codegolf.stackexchange.com/a/28160
instead ofhttp://codegolf.stackexchange.com/a/28160/12551
\$\endgroup\$Justin– Justin2014年05月18日 00:36:24 +00:00Commented May 18, 2014 at 0:36 -
\$\begingroup\$ Chrome doesn't like this: "Uncaught TypeError: Cannot read property 'document' of undefined" \$\endgroup\$Spedwards– Spedwards2014年05月19日 07:00:07 +00:00Commented May 19, 2014 at 7:00
-
\$\begingroup\$ @Spedwards you should disable the popup blocker. \$\endgroup\$nderscore– nderscore2014年05月19日 15:55:56 +00:00Commented May 19, 2014 at 15:55
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
.
Shell and xmllint, 82 bytes
xmllint --xpath 'string(//pre)' http://codegolf.stackexchange.com/posts/28333/body
Swift 5.10 (macOS/iPadOS), (削除) 174 (削除ここまで) 176 bytes
import Foundation;print(try{String.init}()(URL(string:"http://codegolf.stackexchange.com/posts/272295/body")!).matches(of:#/im.+;/#)[0].output.replacing(#/.quot;/#,with:"\""));
This uses pretty much the same trick as this Ruby answer.
It seems that the lack of a newline after import Foundation
broke Stack Exchange's syntax highlighter... (to be fair, though, Xcode acts the same way).
The Linux port of Foundation doesn't yet have networking support (attempts to use networking APIs will crash at runtime), so this only works on Darwin platforms for the time being.
Explore related questions
See similar questions with these tags.