我使用了Py-Stackexchange 提供的问题演示来回答这个问题。
您需要做的最重要的事情是确保您的过滤器包含up_vote_count
和down_vote_count
属性。
拥有此过滤器后,您可以通过question.up_vote_count
(或者answer.up_vote_count
如果您正在检查答案)访问该值。
例如,我修改了演示中的第 22 行,将这两个属性包含在过滤器中:
question = site.question(id, filter="!b0OfMwwD.s*79x")
过滤器可以在这里创建。
然后我在脚本的最后添加了这一行:
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.