-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb-scrape2.R
69 lines (50 loc) · 1.78 KB
/
web-scrape2.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#install package rvest
install.packages("rvest")
#load the libraries
library(rvest)
library(RCurl)
library(XML)
library(magrittr)
library(dplyr)
#extracting links to american film acresses
url<-"https://en.wikipedia.org/wiki/List_of_American_film_actresses"
url2<-getURL(url)
parsed<-htmlParse(url2)
links<-xpathSApply(parsed,path = "//a",xmlGetAttr,"href")
df <- as.data.frame(matrix(unlist(links), nrow=2011, byrow=T),stringsAsFactors=FALSE)
string <- as.character(df$V1)
df <- as.data.frame(paste0("https://en.wikipedia.org", string))
colnames(df) <- "list"
#create empty data frame
data <- as.data.frame(matrix(ncol=1))
#define column names
cols <-c('topic')
#assign column names
colnames(data) <-c(cols)
# df <- read.csv("D:/Ongair/tal/Wikipedia/actresses.csv")
for(i in 1:nrow(df)) {
tryCatch({
row <- as.character(df[i,])
webpage <- read_html("https://en.wikipedia.org/wiki/Amy_Adams")
results <- webpage %>% html_nodes("table.vcard") %>% html_table(trim = TRUE) %>% ifelse(. == "", NA, .)
if (is.logical(results) && length(results) == 0) next
# a <- bind_rows(lapply(xml_attrs(results), function(x) data.frame(as.list(x), stringsAsFactors=FALSE)))
#
# if(!is.data.frame(a) || !nrow(a)) next
table <- webpage %>%
html_nodes("table.vcard") %>%
html_table(header=F)
table <- table[[1]]
# You can clean up the table with the following code, or something like it.
# table[[1]]
dict <- as.data.frame(table)
dict[1, 1] <- NA
if(dict[3,1]=="Born"){
dict <- dict[-c(2), ]
}
colnames(dict) <- c("topic",dict[1,2])
dict <- dict[-1,]
data <- merge(data, dict, by="topic", all = T)
}, error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
print(i)
}