Skip to main content
 首页 » 编程设计

python之使用 Py-Stackexchange 获得答案的赞成票

2025年05月04日10zdz8207

我正在使用 Py_Stackexchange 从 Stackoverflow 中提取数据进行一些统计分析,然后偶然发现了一个问题。

我需要检索对答案的赞成票和反对票。我有 stackexchange.Answer 对象,它有一个名为“transfers”的字段,它是一个字符串元组,例如:

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

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

请您参考如下方法:

我使用了 question演示,由 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") 

可以创建过滤器 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.