[Question]
  [
Write a software that prints on stdout the number of answers and the number of comments(visible and collapsed of question and answers) of this question/page.
Your script must run with this page closed when it starts.
Expected example output:
```
A12C40
```
Where A stands for Answers and C for Comments.
  
[Answer]
# Perl, 91 96  92 chars
```
$_=`curl -sL qr.net/_9`;s/<[^>]+nt="(.+)/$c+=$1/ge;say"A",s/<td.*"ans//g,C,$c+s/<tr.*"com//g
```
---
Some stuff just to break solutions of others ha ha ha :-P
show **93** more comments
href
nt="99" (ha ha crash @Fez Vrasta)
a
,show **99**show **99** ha ha
[href](http://bit.ly/answercell)
[href](https://codegolf.stackexchange.com/questions/20277/answercell)
[href](https://codegolf.stackexchange.com/questions/20277/comment)
ha ha haha :-D
jeeez, just broke my own answer! I discovered that one of the above tricks, which I thought does nothing, will start working *after* this answer is not edited for some time! So your solutions will work only for some time. That's why you had seen +1 more answer in your solutions.. It's a timed bomb! My answer is already prone to it :-) Aaah, found a way how to launch it NOW...
[Answer]
### XQuery, 169, 160, 165
```
let$d:=html:parse(fetch:binary('http://qr.net/1_'))return"A"||count($d//*[@class="answer"])||"C"||count($d//*[@class="comment"])+sum($d//*[@class="comments-link"]/b)
```
More readable (with spaces):
```
let $d:= html:parse(fetch:binary('http://qr.net/1_'))
return "A" || count($d//*[@class="answer"]) || "C" || count($d//*[@class="comment"]) + sum($d//*[@class="comments-link"]/b)
```
[BaseX](http://basex.org) was used as XQuery processor.
[Answer]
# Python 3, 180
```
import lxml.html as h
s=h.parse('http://qr.net/1_').find('body').cssselect
print('A',len(s('.answer')),'C',len(s('.comment'))+sum(int(e.text)for e in s('.comments-link b')),sep='')
```
I'm assuming that this question won't have multiple pages of answers.
[Answer]
## BASH + AWK ~~163~~, ~~144~~, ~~138~~, ~~111~~, ~~110~~, ~~114~~, ~~131~~, ~~132~~, 105
```
curl -sL http://qr.net/_9|awk -F'[<>]' '/^[\t]*>s/{c+=$4}/<tr.*"c/{++c}/<t.*"a/{++a}END{print "A"a"C"c}'
```
Which is the same as this, but without redirecting to a file:
```
curl -sL http://qr.net/_9>f
awk -F'[<>]' '/^[\t]*>s/{c+=$4}/<tr.*"c/{++c}/<t.*"a/{++a}END{print "A"a"C"c}' f
```
### Current output
```
A16C76
```
### Explanation
curl
Transfer a URL.
* `-s` in `curl` is for silent. And `-L` to follow redirects.
awk
To parse the file. As some answers had some code to break other answers, the parsing has been changed so that it parses from the beginning of the line `(^`) to make sure it is not broken.
* `-F'[<>]'` set field separators as `<` or `>`. This way the text can be parsed properly for the "show XXX more comments".
* `/^[\t]*>show <b>/{c+=$4}` on lines containing "spaces....>show", get the 4th field (based on `<`, `>` separators) and add the value to the comments counter.
* `/^[ ]*<tr.*s="comm/{++c}` on lines containing "spaces... 
* `/^<td.*rcell">/{++a}` on lines containing "", increment the counter of answers.
* `END{print "A"a"C"c}` print the output.
[Answer]
## PHP which actually works (302 chars)
Unlike all of the other answers so far, this returns the correct answer even when the question spills onto more than one page.
```
<?function g($a,$b,$i){return json_decode(gzinflate(substr(file_get_contents("http://api.stackexchange.com/2.1/$a/$i/$b?site=codegolf"),10,-8)))->items;}$i=array(20277);foreach(g("questions","answers",20277)as$x)$i[]=$x->answer_id;echo"A".(count($i)-1)."C".count(g("posts","comments",implode(";",$i)));
```
[Answer]
### R, 326
```
library(XML);b=htmlParse("https://codegolf.stackexchange.com/questions/20277");z=xpathApply;x=do.call(sum,sapply(z(b,"//tbody",xmlAttrs),function(x)as.integer(x[[1]])))+length(z(b,"//tr[@class='comment']",xmlValue));y=gsub("[^0-9]","",z(b,"//div[@class='subheader answers-subheader']/h2",xmlValue)[[1]]);cat("A",y,"C",x,sep="")
```
With indentation and explanations:
```
library(XML)
b=htmlParse("https://codegolf.stackexchange.com/questions/20277")
z=xpathApply
x=do.call(sum,sapply(z(b,"//tbody",xmlAttrs),  #Take the first attribute of tag tbody
                     function(x)as.integer(x[[1]]))) #And sum them (=nb of hidden comments
  +length(z(b,"//tr[@class='comment']",xmlValue)) #+nb of visible comments
y=gsub("[^0-9]","", #This is more straightforward as the number of answers is given on front page.
        z(b,"//div[@class='subheader answers-subheader']/h2",xmlValue)[[1]])
cat("A",y,"C",x,sep="")
```
Tested with [this page](https://codegolf.stackexchange.com/questions/20034/the-answer-to-life-the-universe-and-everything), it gives the right number of comments (including hidden) **on the front page** and the right number of answers, i. e. `A23C63`.
And here is a solution at **482 characters** that grab the correct number of comments if the question ends up spreading on several pages:
```
library(XML);h=htmlParse;z=xpathApply;v=xmlValue;a=xmlAttrs;s=sapply;c="http://codegolf.stackexchange.com";f=function(b,i){do.call(sum,s(z(b,"//tbody",a)[i],function(x)as.integer(x[[1]])))+length(z(b,"//tr[@class='comment']",v))};b=h(paste0(c,"/questions/20277"));x=f(b);u=unique(s(z(b,"//div[@class='pager-answers']/a",a),`[`,1));if(length(u))x=x+sum(s(u,function(x)f(h(paste0(c,x)),-1)));y=gsub("[^0-9]","",z(b,"//div[@id='answers-header']/div/h2",v)[[1]]);cat("A",y,"C",x,sep="")
```
Indented:
```
library(XML)
h=htmlParse
z=xpathApply
v=xmlValue
a=xmlAttrs
s=sapply
c="http://codegolf.stackexchange.com"
f=function(b,i){do.call(sum,s(z(b,"//tbody",a)[i],function(x)as.integer(x[[1]])))+length(z(b,"//tr[@class='comment']",v))}
b=h(paste0(c,"/questions/20277"))
x=f(b)
u=unique(s(z(b,"//div[@class='pager-answers']/a",a),`[`,1)) #Grab all URLS of pages
if(length(u))x=x+sum(s(u,function(x)f(h(paste0(c,x)),-1))) #Apply comment computation of all URLs
y=gsub("[^0-9]","",z(b,"//div[@id='answers-header']/div/h2",v)[[1]])
cat("A",y,"C",x,sep="")
```
Tried on [this question](https://codegolf.stackexchange.com/questions/17005/produce-the-number-2014-without-any-numbers-in-your-source-code) and outputted: `A125C499`.
[Answer]
## HTML, 37
*Sorry, Blatant rule abuse follows!*
```
<script src=http://q0x.eu/1></script>
```
### Explanation
`q0x.eu/1` redirects to: [http://api.stackexchange.com/2.1/questions/20277/comments?site=codegolf&callback=...](http://api.stackexchange.com/2.1/questions/20277/comments?site=codegolf&callback=%28function%28d%29%7Bc=d.items.length;document.write%28%27%3Cscript%20src=%22http://q0x.eu/2%22%3E%3C/script%3E%27%29%7D%29)
where the callback is:
```
(function(d){
    c=d.items.length;
    document.write('<script src="http://q0x.eu/2"></script>')
})
```
`q0x.eu/2` redirects to [http://api.stackexchange.com/2.1/questions/20277/answers?site=codegolf&callback=...](http://api.stackexchange.com/2.1/questions/20277/answers?site=codegolf&callback=%28function%28d%29%7Ba=0;g=%5B%5D;d.items.map%28function%28f%29%7Ba%2b%2b;g.push%28f.answer_id%29%7D%29;document.write%28%27%3Cscript%20src=%22http://q0x.eu/3?n=%27%2bg.pop%28%29%2b%27%22%3E%3C/script%3E%27%29%7D%29)
```
(function(d){
    a=0;
    g=[];
    d.items.map(function(f){
        a++;
        g.push(f.answer_id)
    });
    document.write('<script src="http://q0x.eu/3?n='+g.pop()+'"></script>')
})
```
and `q0x.eu/3?n=...` redirects to [http://api.stackexchange.com/2.1/answers/.../comments?site=codegolf&callback=...](http://api.stackexchange.com/2.1/answers/.../comments?site=codegolf&callback=%28function%28d%29%7Bc%2b=d.items.length;g.length?document.write%28%27%3Cscript%20src=%22http://q0x.eu/3?n=%27%2bg.pop%28%29%2b%27%22%3E%3C/script%3E%27%29%3aalert%28%27A%27%2ba%2b%27C%27%2bc%29%7D%29)
```
(function(d){
    c+=d.items.length;
    g.length ? document.write('<script src="http://q0x.eu/3?n='+g.pop()+'"></script>') : alert('A'+a+'C'+c)
})
```
I was originally trying to do it legitimately and might still have a go, but this was fun nevertheless!
[Answer]
## Ruby 175 (counts across pages, using the API instead of the DOM)
```
require'open-uri';require'json'
q=JSON.parse(open("http://qr.net/oyJn").read)["items"][0];a=q["answers"]
puts"A#{a.count}C#{[q,*a].reduce(0){|m,o|m+o["comments"].to_a.count}}"
```
That's 242 without the shortened url:
```
require'open-uri';require'json'
q=JSON.parse(open("http://api.stackexchange.com/2.1/questions/20277?site=codegolf&filter=!azbR89z2Zw*dg.").read)["items"][0]
a=q["answers"]
puts"A#{a.count}C#{[q,*a].reduce(0){|m,o|m+o["comments"].to_a.count}}"
```
Previous 291 answer:
```
require'open-uri';require'json'
def g(o,l,f);JSON.parse(open("http://api.stackexchange.com/2.1/#{o}/#{l}/#{f}?site=codegolf").read)["items"];end
q=20277
p=g("questions",q,"answers").inject([q]){|m,o|m<<o["answer_id"]}
puts"A#{p.count-1}C#{p.map{|i|g("posts",i,"comments").count}.reduce(:+)}"
```
Credits to Peter Tailor for the idea of using the API, and Charles for pointing towards a better API.
[Answer]
## Python with [stackpy](https://pypi.python.org/pypi/stackpy) 160
**Implementation**
```
s=__import__("stackpy").Site("codegolf");q=s.questions(20277);a=q.answers
print"A%dC%d"%(len(a),sum(len(s.answers(e.id()).comments)for e in a)+len(q.comments))
```
**Output**
```
A13C60
```
**Note**
Yesterday I contemplated in using the stackexchange API but took some time for me to understand how it works. Today, I saw there were couple of answers on the same theme. To make my answer a bit different, I though of using an external library. 
Also realize that other answers which relies on parsing for patterns like
are likely to break at anytime soon, so a more definite answer is to rely on a robust method like this one.
[Answer]
## R 239
```
library(XML);F=function(x,y,f=length,z='')sum(as.double(xpathSApply(htmlParse('http://qr.net/1_'),sprintf('//%s[@class="%s"]%s',x,y,z),f)));cat("A",F("div","answer"),"C",F("a","comments-link ",xmlValue,"//b")+F("td","comment-text"),sep="")
```
After I posted my answer, the output is:
```
A13C60
```
[Answer]
Cannot believe that nobody has came up with it until now! Most direct solution to use :-)
# jQuery, 116 101 chars (off competition)
Perhaps this doesn't go with the rules, I keep it just for fun - jQuery solution can't be missing :) At least as a reference to test your scripts!!! ;-)
Try running from the FireBug console:
```
$('.comments-link').click();
setTimeout("alert('A'+$('.answer').length+'C'+$('.comment').length)",999)
```
If you have slow connection, increase the timeout :-) Thanks @Fez Vrasta for the great idea of clicking the "show more" links!
---
Some other stuff to break solution of others, class="comment" and the timed bomb
[ha](http://bit.ly/answercell)
[ha](http://bit.ly/comment)
[Answer]
## PHP: ~~184~~ 172
```
<?$h=file_get_contents('http://qr.net/_9');preg_match_all('/<t.*nt="([0-9]*)/',$h,$c);echo 'A'.substr_count($h,'rcell">').'C'.(array_sum($c[1])+substr_count($h,'mment">'));
```
Explanation:
```
<? // short open tag
  $h = file_get_contents('http://qr.net/_9'); // store in $h the content of the shortened url of the page
  preg_match_all('/<t.*nt="([0-9]*)/', $h, $c); // find each "show/hide X more comments" and store the numbers in $c
  echo
    'A' // output A
    .substr_count($h,'rcell">') // output the count of the occurrences of 'rcell">' (short for '"answercell">')
    .'C' // output C
    .( 
      array_sum( $c[1] ) // output the sum of the collapsed comments found before
      + 
      substr_count( $h, 'mment">') // output the count of the occurrences of 'mment">' (short for '"comment">')
    );
```
*For the first time PHP beats other languages in golf-scripts :')*
---
**Some extra markup to this topic to avoid regex "cheats":** 
[show **9999** more comments](http://goo.gl)
[Answer]
## Node, 403
```
r=require;m='comments'
r('http').get("http://api.stackexchange.com/2.1/questions/20277?site=codegolf&filter=!azbR89z2Zw*dg.").on('response',function(p){p.pipe(r('zlib').createGunzip(o="")).on('readable',function(){o+=this.read()}).on('end',function(){d=JSON.parse(o).items[0]
r('util').print("A",d.answer_count,"C",(d[m].length+d.answers.reduce(function(p,c){return p+(c[m]?c[m].length:0)},0)))})})
```
Only hits the API once... can likely be shortened, but I'm new to Node.
[Answer]
## 153 151 147, C# & CsQuery
C# With CsQuery:
```
    var d = CsQuery.CQ.CreateFromUrl("http://qr.net/1_");
    Console.Write("A" + d[".answer"].Count() + "C" + d[".comment"].Count());
```
Full program:
```
class P{static void Main(){var d =CsQuery.CQ.CreateFromUrl("http://qr.net/1_");Console.Write("A"+d[".answer"].Count()+"C"+d[".comment"].Count());}}
```
### 118 C# & CsQuery in LINQPad or in Roslyn
If running through LINQPad is allowed then:
```
var d =CsQuery.CQ.CreateFromUrl("http://qr.net/1_");Console.Write("A"+d[".answer"].Count()+"C"+d[".comment"].Count());
```
Produces:
> 
> A14C48
> 
> 
> 
Some more fun.
### F# with CsQuery, 143:
```
[<EntryPoint>]
let main x= 
 let d=CsQuery.CQ.CreateFromUrl("http://qr.net/1_")
 printfn "A%dC%d" d.[".answer"].Length d.[".comment"].Length
 0 
```
]