この質問/ページの回答数とコメント数(質問と回答の表示と折りたたみ)を標準出力に印刷するソフトウェアを作成します。
スクリプトは、開始時にこのページを閉じて実行する必要があります。
期待される出力例:
A12C40
AはAnswers、Cはコメントを表します。
この質問/ページの回答数とコメント数(質問と回答の表示と折りたたみ)を標準出力に印刷するソフトウェアを作成します。
スクリプトは、開始時にこのページを閉じて実行する必要があります。
期待される出力例:
A12C40
AはAnswers、Cはコメントを表します。
回答:
$_=`curl -sL qr.net/_9`;s/<[^>]+nt="(.+)/$c+=$1/ge;say"A",s/<td.*"ans//g,C,$c+s/<tr.*"com//g
いくつかの他のものの解決策を破るためだけのものはハッハッハ:-P
表示さ93件の以上のコメントを
href
nt = "99"(ha ha crash @Fez Vrasta)
表示99ショー99は、ハハ
href href href ha ha haha :-D
いや、私自身の答えを破った!私は何もしないと思った以上のトリック、、のいずれかの作業を開始することを発見した後、この答えは、いくつかの時間のために編集されていません!したがって、ソリューションはしばらくの間のみ機能します。ソリューションでさらに+1の回答を見た理由です。これは時限爆弾です。私の答えはすでにその傾向があります:-)ああ、今すぐ起動する方法を見つけました...
perl -E
ます。コメントを削除します。また、あなたはもうそれを
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)
より読みやすい(スペース付き):
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はXQueryプロセッサとして使用されました。
doc()
このページが有効なXMLである場合は、(関数を使用して)さらに短くなります...
$d//*[@class="comments-link"]/b
(<B>タグの中に隠されたコメントの数が示されている)のためである
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='')
この質問には複数ページの回答がないことを前提としています。
A13C20
ます。正規表現に角括弧を含めると、それらはエスケープされるので良いと思います。
,sep=''
curl -sL http://qr.net/_9|awk -F'[<>]' '/^[\t]*>s/{c+=$4}/<tr.*"c/{++c}/<t.*"a/{++a}END{print "A"a"C"c}'
これはこれと同じですが、ファイルにリダイレクトしません:
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
A16C76
URLを転送します。
-s
in curl
はサイレントです。そして-L
リダイレクトに従う。ファイルを解析します。一部の回答には他の回答を(^
無効にするコードが含まれていたため、構文解析が変更され、行の先頭から解析されて、破損しないようになっています。
-F'[<>]'
フィールド区切り文字を<
またはに設定します>
。このようにして、「show XXX more comments」のテキストを適切に解析できます。/^[\t]*>show <b>/{c+=$4}
「スペース....>ショー」を含む行の上に、(に基づいて第4フィールドを取得<
、>
セパレータ)とコメントカウンタに値を追加します。/^[ ]*<tr.*s="comm/{++c}
「スペース...」を含む行
/^<td.*rcell">/{++a}
「」を含む行では、回答のカウンターを増やします。END{print "A"a"C"c}
出力を印刷します。m
と思うf
:するecho "A$(grep -c 'rcell">' m)C$(grep -c 'mment">' f)"
必要がecho "A$(grep -c 'rcell">' f)C$(grep -c 'mment">' f)"
ありますか?
これまでの他のすべての回答とは異なり、質問が複数のページにあふれた場合でも、正しい回答が返されます。
<?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)));
(count($i)-1)
質問を含まないようにカウントを修正します。
[]
代わりにarray()
初期化するために
$i
れたアイテムが100を超える場合、これは実際には機能しません。(私の独自のバージョンのrubyで同じことが、この制限はありませんが、確かにそれはAPIの制限にぶつかることがあり;-)。。)
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="")
インデントと説明付き:
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="")
このページでテストすると、フロントページに適切な数のコメント(非表示を含む)と適切な数の回答、つまりが表示されA23C63
ます。
そして 、質問が複数のページに広がった場合に正しい数のコメントを取得する482文字のソリューションを次に示します。
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="")
インデント:
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="")
この質問を試して出力しました:A125C499
。
申し訳ありませんが、Blatantルールの乱用が続きます!
<script src=http://q0x.eu/1></script>
q0x.eu/1
リダイレクト先:http://api.stackexchange.com/2.1/questions/20277/comments?site = codegolf&callback = ...
コールバックは次のとおりです。
(function(d){
c=d.items.length;
document.write('<script src="http://q0x.eu/2"></script>')
})
q0x.eu/2
http://api.stackexchange.com/2.1/questions/20277/answers?site = codegolf&callback = ...にリダイレクトします
(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>')
})
とq0x.eu/3?n=...
にリダイレクトのhttp:?//api.stackexchange.com/2.1/answers/.../commentsサイト= codegolf&コールバック= ...
(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)
})
私はもともとそれを合法的にやろうとしていたので、まだやることがあるかもしれませんが、それでも楽しかったです!
q0x.eu/1
でしょうか。PS:正当な解決策はとにかくPerlに勝るものはありません;
curl q0x.eu/1
...か何かが、あまりにも怠惰だった
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}}"
これは短縮URLなしでは242です。
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}}"
前の291の答え:
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(:+)}"
APIを使用するアイデアについてはPeter Tailorに、より良いAPIを示すためにCharlesに感謝します。
実装
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))
出力
A13C60
注意
昨日、stackexchange APIの使用を検討しましたが、それがどのように機能するかを理解するのに時間がかかりました。今日、私は同じテーマに関するいくつかの答えがあるのを見ました。私の答えを少し変えるために、外部ライブラリを使用しました。
また、次のようなパターンの解析に依存する他の答えも理解してください
すぐに壊れる可能性が高いため、より明確な答えは、このような堅牢な方法に依存することです。
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="")
回答を投稿した後の出力は次のとおりです。
A13C60
F("div","answer")
そして、F("td","comment-text")
(私のために動作しませんError in F("div", "answer") : (list) object cannot be coerced to type 'double'
)。デフォルトではすべきではないためにf
もfunction(x)length(xmlValue(x))
代わりのlength
、または似たような?
xpathSApply(...,length)
リストではなくベクトルを返す必要があります。なぜリストを取得するのかわかりません。そして、いや、私は実際に使用することを意味length
していないfunction(x)length(xmlValue(x))
:目標は、のベクトルを取得することで1
、S、各コメントまたは回答のための1つを。
xpathSApply
他の機能(などlength
)を使用できるという事実xmlValue
と、同様の機能がパッケージのバージョン3.94からの目新しさであることがわかりました(変更ログを正しく理解している場合)。それが私にとってうまくいかなかった理由かもしれません。ええ、私はあなたが1のベクトルを望んでいるというアイデアを得ましたが、パッケージの私のバージョンではfunction(...
、ただの代わりにそれを達成することができlength
ます。
今まで誰もそれを思いついたとは信じられない!使用する最も直接的なソリューション:-)
おそらくこれはルールに合わないでしょう、私はただ楽しみのためにそれを保持します-jQueryソリューションは見逃せません:)少なくともあなたのスクリプトをテストするためのリファレンスとして!!! ;-)
FireBugコンソールから実行してみてください。
$('.comments-link').click();
setTimeout("alert('A'+$('.answer').length+'C'+$('.comment').length)",999)
接続が遅い場合は、タイムアウトを増やしてください:-)「もっと表示」リンクをクリックするという素晴らしいアイデアを@Fez Vrastaに感謝します!
<?$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">'));
説明:
<? // 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">')
);
PHPが初めてゴルフスクリプトで他の言語に勝る: ')
正規表現の「チート」を回避するための、このトピックへの追加のマークアップ:
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)))})})
APIにヒットするのは1回だけです...短縮できる可能性がありますが、Nodeは初めてです。
CsQueryを使用したC#:
var d = CsQuery.CQ.CreateFromUrl("http://qr.net/1_");
Console.Write("A" + d[".answer"].Count() + "C" + d[".comment"].Count());
完全なプログラム:
class P{static void Main(){var d =CsQuery.CQ.CreateFromUrl("http://qr.net/1_");Console.Write("A"+d[".answer"].Count()+"C"+d[".comment"].Count());}}
LINQPadの実行が許可されている場合:
var d =CsQuery.CQ.CreateFromUrl("http://qr.net/1_");Console.Write("A"+d[".answer"].Count()+"C"+d[".comment"].Count());
生産物:
A14C48
もう少し楽しい。
[<EntryPoint>]
let main x=
let d=CsQuery.CQ.CreateFromUrl("http://qr.net/1_")
printfn "A%dC%d" d.[".answer"].Length d.[".comment"].Length
0
コンソールに貼り付けてください。1ページしかないと仮定します。
(function(){var n=$(".comment").length;$(".comments-link b").each(function(g,f){n+=parseInt($(f).html())});return"A"+$("#answers .answer").length+"C"+n})()