我正在尝试使用 rvest 从网站上抓取 HTML 表格。唯一的问题是我要抓取的表没有 <tr>
标签,第一行除外。它看起来像这样:
<tr>
<td>6/21/2015 9:38 PM</td>
<td>5311 Lake Park</td>
<td>UCPD</td>
<td>African American</td>
<td>Male</td>
<td>Subject was causing a disturbance in the area.</td>
<td>Name checked; no further action</td>
<td>No</td>
</tr>
<td>6/21/2015 10:37 PM</td>
<td>5200 S Blackstone</td>
<td>UCPD</td>
<td>African American</td>
<td>Male</td>
<td>Subject was observed fighting in the McDonald's parking lot</td>
<td>Warned; released</td>
<td>No</td>
</tr>
等等。因此,使用以下代码,我只能将第一行放入我的数据框中:
library(rvest)
mydata <- html_session("https://incidentreports.uchicago.edu/incidentReportArchive.php?startDate=06/01/2015&endDate=06/21/2015") %>%
html_node("table") %>%
html_table(header = TRUE, fill=TRUE)
我怎样才能改变这个让 html_table 理解行是行,即使它们没有开口
<tr>
标签?或者有没有更好的方法来解决这个问题?
最佳答案
library(rvest)
url_parse<- read_html("https://incidentreports.uchicago.edu/incidentReportArchive.php?startDate=06/01/2015&endDate=06/21/2015")
col_name<- url_parse %>%
html_nodes("th") %>%
html_text()
mydata <- url_parse %>%
html_nodes("td") %>%
html_text()
finaldata <- data.frame(matrix(mydata, ncol=7, byrow=TRUE))
names(finaldata) <- col_name
finaldata
Incident Location
Reported Occurred
1 Theft 1115 E. 58th St. (Walker Bike Rack) 6/1/15 12:18 PM 5/31/15 to 6/1/15 8:00 PM to 12:00 PM
2 Information 5835 S. Kimbark 6/1/15 3:57 PM 6/1/15 3:55 PM
3 Information 1025 E. 58th St. (Swift) 6/2/15 2:18 AM 6/2/15 2:18 AM
4 Non-Criminal Damage to Property 850 E. 63rd St. (Car Wash) 6/2/15 8:48 AM 6/2/15 8:00 AM
5 Criminal Damage to Property 5631 S. Cottage Grove (Parking Structure) 6/2/15 7:32 PM 6/2/15 6:45 PM to 7:30 PM
Comments / Nature of Fire Disposition
1 Bicycle secured to bike rack taken by unknown person Open
2 Unknown person used staff member's personal information to file a fraudulent claim with U.S. Social Security Admin. / CPD case CPD
3 Three unaffiliated individuals reported tampering with bicycles in bike rack / Subjects were given trespass warnings and sent on their way Closed
4 Rear wiper blade assembly damaged on UC owned vehicle during car wash Closed
5 Unknown person(s) spray painted graffiti on north concrete wall of the structure Open
UCPDI#
1 E00344
2 E00345
3 E00346
4 E00347
5 E00348
关于html - R - 当缺少 <tr> 标签时,使用 rvest 抓取 HTML 表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30989543/