【发布时间】:2018-02-15 04:20:04
【问题描述】:
我是 R 的新手。我昨天抓取了一个需要登录的网站,页面是 xml 格式,如下所示。
<result status="success">
<code>1</code>
<note>success</note>
<teacherList>
<teacher id="D95">
<name>Mary</name>
<department id="420">
<name>Math</name>
</department>
<department id="421">
<name>Statistics</name>
</department>
</teacher>
<teacher id="D73">
<name>Adam</name>
<department id="412">
<name>English</name>
</department>
</teacher>
</teacherList>
</result>
最近我刚刚将 XML 转换为列表。
library(XML)
library(rvest)
library(plyr)
library(dplyr)
library(httr)
library(pipeR)
library(xml2)
url.address <- "http://xxxxxxxxxxxxxxxxx"
session <-html_session(url.address)
form <-html_form(read_html(url.address))[[1]]
filled_form <- set_values(form,
"userid" = "id",
"Password" = "password")
s <- submit_form(session,filled_form)
z = read_xml(s$response)
z1 = as_list(z)
z2 <- z1$teacherList
现在我需要从列表中提取数据并将其作为数据框。顺便说一句,有些人属于 2 个部门,但有些人只属于 1 个部门。列表 z2 的一部分如下所示:
z2[[1]]
$name
$name[[1]]
[1] "Mary"
$department
$department$name
$department$name[[1]]
[1] "Math"
attr(,"id")
[1] "420"
$department
$department$name
$department$name[[1]]
[1] "statistics"
attr(,"id")
[1] "421"
attr(,"id")
[1] "D95236"
当我一一提取时,时间太长了:
attr(z2[[1]],"id")
“D95”
z2[[1]][[1]][[1]]
“玛丽”
z2[[1]][[2]][[1]][[1]]
“数学”
attr(z2[[1]][[2]], "id")
“420”
z2[[1]][[3]][[1]][[1]]
“统计”
attr(z2[[1]][[3]], "id")
“421”
attr(z2[[2]],"id")
“D73”
z2[[2]][[1]][[1]]
“亚当”
z2[[2]][[2]][[1]][[1]]
“英语”
attr(z2[[2]][[2]],"id")
“412”
于是我试着写了一个循环:
for (x in 1:2){
for (y in 2:3){
a <- attr(z2[[x]],"id")
b <- z2[[x]][[1]][[1]]
d <- z2[[x]][[y]][[1]][[1]]
e <- attr(z2[[x]][[y]],"id")
g <- cbind(print(a),print(b),print(d),print(e))
}}
但它根本不起作用,因为有些人只属于一个部门。我预期的结果:
任何建议将不胜感激!
dput(head(z2, 10))
structure(list(teacher = structure(list(name = list("Mary"),
department = structure(list(name = list("Math")), .Names = "name", id = "420"),
department = structure(list(name = list("statistics")), .Names = "name", id = "421")), .Names = c("name",
"department", "department"), id = "D95"), teacher = structure(list(
name = list("Adam"), department = structure(list(name = list(
"English")), .Names = "name", id = "412")), .Names = c("name",
"department"), id = "D73"), teacher = structure(list(name = list(
"Kevin"), department = structure(list(name = list("Chinese")), .Names = "name", id = "201")), .Names = c("name",
"department"), id = "D101"), teacher = structure(list(name = list(
"Nana"), department = structure(list(name = list("Science")), .Names = "name", id = "205")), .Names = c("name",
"department"), id = "D58"), teacher = structure(list(name = list(
"Nelson"), department = structure(list(name = list("Music")), .Names = "name", id = "370")), .Names = c("name",
"department"), id = "D14"), teacher = structure(list(name = list(
"Esther"), department = structure(list(name = list("Medicine")), .Names = "name", id = "361")), .Names = c("name",
"department"), id = "D28"), teacher = structure(list(name = list(
"Mia"), department = structure(list(name = list("Chemistry")), .Names = "name", id = "326")), .Names = c("name",
"department"), id = "D17"), teacher = structure(list(name = list(
"Jack"), department = structure(list(name = list("German")), .Names = "name", id = "306")), .Names = c("name",
"department"), id = "D80"), teacher = structure(list(name = list(
"Tom"), department = structure(list(name = list("French")), .Names = "name", id = "360")), .Names = c("name",
"department"), id = "D53"), teacher = structure(list(name = list(
"Allen"), department = structure(list(name = list("Spanish")), .Names = "name", id = "322")), .Names = c("name",
"department"), id = "D18")), .Names = c("teacher", "teacher",
"teacher", "teacher", "teacher", "teacher", "teacher", "teacher", "teacher",
"teacher"))
【问题讨论】:
-
除非您提供可重现的数据示例,否则无法提供帮助。尝试
dput(head(z2, 10))并将结果粘贴到您的问题中。 -
@lmo 对不起!刚刚添加:)
-
请不要粘贴代码图片。请阅读how to make a great reproducible example
-
@lmo 现在就上传吧。对不起,我还没有弄清楚如何发布输出,所以我上传了图片。很抱歉给您带来不便。
-
@C8H10N4O2 嗨!非常抱歉,前两天才开始使用。我知道这不应该成为我的借口。我会尽快弄清楚如何做。