Wolfram Language/Mathematica, 118118 117 bytes
saved a byte thanks to numbermanic
If[ImportString[#,"HTML"]===#,#,$Failed]&@Import[StringTemplate["http://xkcd.com/``/info.0.json"]@#,"RawJSON"]["alt"]&"RawJSON"]@"alt"&
Explanation:
Use StringTemplate
to form the URL from the input.
Import[..., "RawJSON"]
imports the JSON object and parses it into an Assocation
.
Select the value for the key "alt"
.
Take this result and try to interpret the string as HTML (Import[#,"HTML"]
). If this doesn't change anything pass the result through, if it does return $Failed
. This catches 859 because
ImportString[
"Brains aside, I wonder how many poorly-written xkcd.com-parsing
scripts will break on this title (or ;;\"''{<<[' this mouseover text.\"","HTML"]
results in:
Brains aside, I wonder how many poorly-written xkcd.com-parsing
scripts will break on this title (or ;;"''{
404 fails because
If[
ImportString[$Failed["alt"], "HTML"] === $Failed["alt"],
$Failed["alt"],
$Failed]
results in $Failed
.