如何在 Stack Exchange API 中使用自定义过滤器?

Nem*_*nem 3 python stackexchange-api

我正在尝试从 StackApi 获取问题和答案来训练深度学习模型。我有一个问题,我不明白如何使用自定义过滤器,所以我只能得到问题的正文。

\n

这是我的代码:

\n
from stackapi import StackAPI\nimport torch\nimport torch.nn as nn\n\nSITE = StackAPI('stackoverflow')\nSITE.max_pages=1\nSITE.page_size=1\ndata = SITE.fetch('questions', tagged='python',filter = '!*SU8CGYZitCB.D*(BDVIficKj7nFMLLDij64nVID)N9aK3GmR9kT4IzT*5iO_1y3iZ)6W.G*', sort = 'votes')\nfor quest in data['items']:\n    question = quest['title']\n    print(question)\n    question_id = quest['question_id']\n    print (question_id)\n    dataAnswer = SITE.fetch('questions/{ids}/answers', ids=[question_id], filter='withbody')\n    print(dataAnswer)\n
Run Code Online (Sandbox Code Playgroud)\n

我的 dataAnswer 结果:

\n
{'backoff': 0, 'has_more': True, 'page': 1, 'quota_max': 300, 'quota_remaining': 300, 'total': 0, 'items': [{'owner': {'reputation': 404, 'user_id': 11182732, 'user_type': 'registered', 'profile_image': 'https://lh6.googleusercontent.com/-F2a9OP4yGHc/AAAAAAAAAAI/AAAAAAAADVo/Mn4oVgim-m8/photo.jpg?sz=128', 'display_name': 'Aditya patil', 'link': '/sf/users/782791271/'}, 'is_accepted': False, 'score': 8, 'last_activity_date': 1609856797, 'last_edit_date': 1609856797, 'creation_date': 1587307868, 'answer_id': 61306333, 'question_id': 231767, 'content_license': 'CC BY-SA 4.0', 'body': '<p><strong>The yield keyword is going to \nreplace return in a function definition to create a generator.</strong></p>\\n<pre><code>def create_generator():\\n   for i in range(100):\\n   yield i\\nmyGenerator = create_generator()\\nprint(myGenerator)\\n# &lt;generator object create_generator at 0x102dd2480&gt;\\nfor i in myGenerator:\\n   print(i) # prints 0-99\\n</code></pre>\\n<p>When the returned generator is first used\xe2\x80\x94not in the assignment but the for loop\xe2\x80\x94the function definition will execute until it reaches the yield statement. There, it will pause (see why it\xe2\x80\x99s called yield) until used again. Then, it will pick up where it left off. Upon the final iteration of the generator, any code after the yield command will execute.</p>\\n<pre><code>def create_generator():\\n   print(&quot;Beginning of generator&quot;)\\n   for i in range(4):\\n      yield i\\n   print(&quot;After yield&quot;)\\nprint(&quot;Before assignment&quot;)\\n\\nmyGenerator = create_generator()\\n\\nprint(&quot;After assignment&quot;)\\nfor i in myGenerator :\\n   print(i) # prints 0-3\\n&quot;&quot;&quot;\\nBefore assignment\\nAfter assignment\\nBeginning of generator\\n0\\n1\\n2\\nAfter yield\\n</code></pre>\\n<p>The <strong>yield</strong> keyword modifies a function\xe2\x80\x99s behavior to produce a generator that\xe2\x80\x99s paused at each yield command during iteration. The function isn\xe2\x80\x99t executed except upon iteration, \nwhich leads to improved resource management, and subsequently, a better overall performance. Use generators (and yielded functions) for creating large data sets meant for single-use iteration.</p>\\n'}]}\n
Run Code Online (Sandbox Code Playgroud)\n

现在我只想得到结果的主体。我可以withbody用定制过滤器更换过滤器吗?如果可以,更换哪一个?

\n

dou*_*eep 5

  1. 从API 文档中选择您的方法。在这种情况下,就是这个/questions/{ids}/answers
  2. 单击[edit]默认过滤器旁边的 ,编辑所需的字段,然后单击“保存”。
  3. 复制出现的过滤器并将其粘贴到您的代码中。

SE API 文档游乐场中的“过滤器”下拉菜单

由于缺乏该/filters/create方法的(适当的)文档,以编程方式创建过滤器很复杂。由于您想要答案正文,因此您需要包括answer.body其以及默认.wrapper字段包含在过滤器中。例如:

from stackapi import StackAPI

defaultWrapper = '.backoff;.error_id;.error_message;.error_name;.has_more;.items;.quota_max;.quota_remaining;'
includes = 'answer.body'

SITE = StackAPI('stackoverflow')
# See https://stackapi.readthedocs.io/en/latest/user/advanced.html#end-points-that-don-t-accept-site-parameter
SITE._api_key = None
data = SITE.fetch('filters/create', base = 'none', include = defaultWrapper + includes)
print(data['items'][0]['filter'])
Run Code Online (Sandbox Code Playgroud)

你相应地改变的地方includes

参考: