Python 如何在堆栈交换API中使用自定义过滤器?

Python 如何在堆栈交换API中使用自定义过滤器?,python,stackexchange-api,Python,Stackexchange Api,我正在尝试从StackApi获取问题和答案,以训练一个深度学习模型。我有一个问题,我不知道如何使用自定义过滤器,所以我只能得到问题的主体 这是我的代码: from stackapi import StackAPI import torch import torch.nn as nn SITE = StackAPI('stackoverflow') SITE.max_pages=1 SITE.page_size=1 data = SITE.fetch('questions', tagged='p

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

这是我的代码:

from stackapi import StackAPI
import torch
import torch.nn as nn

SITE = StackAPI('stackoverflow')
SITE.max_pages=1
SITE.page_size=1
data = SITE.fetch('questions', tagged='python',filter = '!*SU8CGYZitCB.D*(BDVIficKj7nFMLLDij64nVID)N9aK3GmR9kT4IzT*5iO_1y3iZ)6W.G*', sort = 'votes')
for quest in data['items']:
    question = quest['title']
    print(question)
    question_id = quest['question_id']
    print (question_id)
    dataAnswer = SITE.fetch('questions/{ids}/answers', ids=[question_id], filter='withbody')
    print(dataAnswer)
我的数据回答结果:

{'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': 'https://stackoverflow.com/users/11182732/aditya-patil'}, '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 
replace 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—not in the assignment but the for loop—the function definition will execute until it reaches the yield statement. There, it will pause (see why it’s 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’s behavior to produce a generator that’s paused at each yield command during iteration. The function isn’t executed except upon iteration, 
which 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当返回的生成器第一次不是用于赋值,而是用于for循环时,函数定义将执行,直到到达yield语句。在那里,它将暂停(看看为什么它被称为收益率),直到再次使用。然后,它将继续它停止的地方。在生成器的最后一次迭代中,将执行yield命令之后的任何代码。

\n\n关键字yield修改函数的行为,以生成一个生成器,该生成器在迭代期间的每个yield命令处暂停。该函数只有在迭代时才会执行, 这将改进资源管理,从而提高总体性能。使用生成器(和生成的函数)创建用于一次性迭代的大型数据集。

\n'}]} 现在我只想得到结果的主体。我可以用自定义过滤器替换
withbody
过滤器吗?如果可以,是哪一个

  • 从中选择您的方法。在这种情况下,这是一个
  • 单击默认过滤器旁边的
    [编辑]
    ,编辑所需字段,然后单击保存
  • 复制出现的过滤器并将其粘贴到代码中
  • 由于缺少(适当的)方法文档,以编程方式创建过滤器非常复杂。由于需要答案的正文,因此需要在筛选器中包括
    answer.body
    ,以及默认的
    .wrapper
    字段。例如:

    您更改
    的地方包括相应的

    参考资料:

    • 关于堆栈交换API的文档
    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'])