如何使用 API 的 stackr 库获取问题文本?
How to get questions' text using the stackr library for the API?
使用 stackr 可以通过以下命令列出特定用户的问题。
library(devtools)
devtools::install_github("dgrtwo/stackr")
library(stackr)
textques <- stack_users(712603, "questions", num_pages=10, pagesize=100)
有没有可能每题都有正文?
There is 一个正文选项,但我如何将它与上一个命令一起使用?
此外,我试过 body equals true 但它不起作用:
textques <- stack_users(712603, "questions", body = "true", num_pages=10, pagesize=100)
我不确定这个包是否提供了提取所问问题的功能,但这是一种基于rvest
的方法
for(i in 1:nrow(textques)){
link <- textques$link[i]
textques$qtext[i] <- read_html(link) %>% html_node("#question .post-text") %>% html_text()
}
这应该将另一个变量添加到您现有的数据框中,每个 link.
中都有问题的文本
自从 stack_users()
and stack_GET()
function both accept (and pass on) the ellipsis argument, you can add any parameter onto the end of the API call URL. This includes the filter
parameter, where we can use the built-in withbody
filter:
library(stackr)
questions <- stack_users(9371451, "questions", num_pages=10, pagesize=100, filter="withbody")
for (i in 1:nrow(questions)) {
qtext <- questions$body[i]
print(qtext)
}
输出:
[1] "<p>I would like to use the Stack Exchange API with a specific user id to get the text of a user's badges.</p>\n\n<p>I found <a href=\"https://github.com/dgrtwo/stackr\" rel=\"nofollow noreferrer\">the <em>stackr</em> library for the Stack Exchange API</a>, and tried this:</p>\n\n<pre><code># install.packages(\"devtools\")\nlibrary(devtools)\ndevtools::install_github(\"dgrtwo/stackr\")\nlibrary(stackr)\nstack_users(712603)\n</code></pre>\n\n<p>But that only gives the total number of every kind of badge. How can I take the text from each one?\nExample:</p>\n\n<pre><code>gold silver bronze\nr r ggplot2\n</code></pre>\n\n<p>I don't only want the total number of badges but also what the badges are.</p>\n"
[1] "<p>Using stackr it is possible with the following command to take the question from a specific user.</p>\n\n<pre><code>library(devtools)\ndevtools::install_github(\"dgrtwo/stackr\")\nlibrary(stackr)\ntextques <- stack_users(712603, \"questions\", num_pages=10, pagesize=100)\n</code></pre>\n\n<p>How is it possible to have also the text of every question?</p>\n\n<p><a href=\"https://api.stackexchange.com/docs/types/question\">There is</a> a body option but how could I use it with the previous command</p>\n\n<p>Also I tried body equals true but it is not working:</p>\n\n<pre><code>textques <- stack_users(712603, \"questions\", body = \"true\", num_pages=10, pagesize=100)\n</code></pre>\n"
使用 stackr 可以通过以下命令列出特定用户的问题。
library(devtools)
devtools::install_github("dgrtwo/stackr")
library(stackr)
textques <- stack_users(712603, "questions", num_pages=10, pagesize=100)
有没有可能每题都有正文?
There is 一个正文选项,但我如何将它与上一个命令一起使用?
此外,我试过 body equals true 但它不起作用:
textques <- stack_users(712603, "questions", body = "true", num_pages=10, pagesize=100)
我不确定这个包是否提供了提取所问问题的功能,但这是一种基于rvest
for(i in 1:nrow(textques)){
link <- textques$link[i]
textques$qtext[i] <- read_html(link) %>% html_node("#question .post-text") %>% html_text()
}
这应该将另一个变量添加到您现有的数据框中,每个 link.
中都有问题的文本自从 stack_users()
and stack_GET()
function both accept (and pass on) the ellipsis argument, you can add any parameter onto the end of the API call URL. This includes the filter
parameter, where we can use the built-in withbody
filter:
library(stackr)
questions <- stack_users(9371451, "questions", num_pages=10, pagesize=100, filter="withbody")
for (i in 1:nrow(questions)) {
qtext <- questions$body[i]
print(qtext)
}
输出:
[1] "<p>I would like to use the Stack Exchange API with a specific user id to get the text of a user's badges.</p>\n\n<p>I found <a href=\"https://github.com/dgrtwo/stackr\" rel=\"nofollow noreferrer\">the <em>stackr</em> library for the Stack Exchange API</a>, and tried this:</p>\n\n<pre><code># install.packages(\"devtools\")\nlibrary(devtools)\ndevtools::install_github(\"dgrtwo/stackr\")\nlibrary(stackr)\nstack_users(712603)\n</code></pre>\n\n<p>But that only gives the total number of every kind of badge. How can I take the text from each one?\nExample:</p>\n\n<pre><code>gold silver bronze\nr r ggplot2\n</code></pre>\n\n<p>I don't only want the total number of badges but also what the badges are.</p>\n"
[1] "<p>Using stackr it is possible with the following command to take the question from a specific user.</p>\n\n<pre><code>library(devtools)\ndevtools::install_github(\"dgrtwo/stackr\")\nlibrary(stackr)\ntextques <- stack_users(712603, \"questions\", num_pages=10, pagesize=100)\n</code></pre>\n\n<p>How is it possible to have also the text of every question?</p>\n\n<p><a href=\"https://api.stackexchange.com/docs/types/question\">There is</a> a body option but how could I use it with the previous command</p>\n\n<p>Also I tried body equals true but it is not working:</p>\n\n<pre><code>textques <- stack_users(712603, \"questions\", body = \"true\", num_pages=10, pagesize=100)\n</code></pre>\n"