使用Py-Stackexchange获得答案的upvotes

时间:2014-10-01 14:26:09

标签: python stackexchange-api

我正在使用Py_StackexchangeStackoverflow提取数据进行统计分析,并偶然发现问题。

我需要在答案中检索upvotes和downvotes。我有stackexchange.Answer对象,它有一个名为'transfers'的字段,它是一个字符串元组,如:

'is_accepted', 'locked_date', 'question_id', 'up_vote_count', 'down_vote_count',
'view_count', 'score', 'community_owned', 'title', 'body'

如何获得与这些字段对应的实际数值?

1 个答案:

答案 0 :(得分:1)

我利用Py-Stackexchange提供的question演示来获得这个答案。

您需要做的最重要的事情是确保您的过滤器包含up_vote_countdown_vote_count属性。

获得此过滤条件后,您可以question.up_vote_count(或answer.up_vote_count检查答案时)访问该值。

作为示例,我在演示中修改了第22行,以在过滤器中包含这两个属性:

question = site.question(id, filter="!b0OfMwwD.s*79x")

可以创建过滤器here

然后我在剧本的最后添加了这一行:

print('%d Upvotes.' % question.up_vote_count)

当我针对这个问题运行时,我得到了这个输出:

Please enter an API key if you have one (Return for none):
Enter a question ID: 26143702
--- Getting the upvotes of an answer using Py-Stackexchange ---
<p>I'm using <code>Py_Stackexchange</code> to pull data from <code>Stackoverflow</code> for some statistical analysis, and stumbled upon a problem.</p>

<p>I need to retrieve the upvotes and downvotes on an answer. I have the <code>stackexchange.Answer</code> object, and it has a field called 'transfers' which is a tuple of strings like:</p>

<pre><code>'is_accepted', 'locked_date', 'question_id', 'up_vote_count', 'down_vote_count',
'view_count', 'score', 'community_owned', 'title', 'body'
</code></pre>

<p>How do I get the actual numerical values corresponding to these fields?</p>


0 answers.
1 Upvotes.