-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDocketScraper.R
187 lines (157 loc) · 7.74 KB
/
DocketScraper.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
## DOCKET SCRAPER - BETA VERSION
pacman::p_load(rvest, xml2, tidyverse, magrittr, tm)
#### Selecting from the Search Type Menu ####
selectDropdown <- function(remoteDriver, node, selection) {
dropdownOptions <- xml2::read_html(remoteDriver$getPageSource()[[1]]) %>% #read html page
rvest::html_nodes(node) %>% #go to node
rvest::html_children() %>% #find the children of the node
rvest::html_text() %>% #pull out the text
dplyr::data_frame(options = .) #create dataframe
numOfOptions <- dim(dropdownOptions)[1]
dropdownOptions <-dropdownOptions %>%
dplyr::mutate(listPosition = 1:numOfOptions,
x = paste0(node, " > option:nth-child(", listPosition, ")"))
selectedIndex <- grep(selection,
dropdownOptions$options,
ignore.case = T)
remoteDriver$findElement(using = 'css selector',
dropdownOptions$x[selectedIndex])$clickElement()
Sys.sleep(1)
}
scrapeForDockets <- function(remoteDriver, id, lastName, firstName, dateOfBirth) {
print(paste("Processing", firstName, lastName))
parentNode <- "#ctl00_ctl00_ctl00_cphMain_cphDynamicContent_cphDynamicContent_"
parentNode <- paste0(parentNode, "participantCriteriaControl_")
#### Filling in Name ####
lastNameNode <- paste0(parentNode, "lastNameControl")
element <- remoteDriver$findElement(using = "css selector",
lastNameNode)
element$clearElement()
element$sendKeysToElement(list(lastName))
firstNameNode <- paste0(parentNode, "firstNameControl")
element <- remoteDriver$findElement(using = "css selector",
firstNameNode)
element$clearElement()
element$sendKeysToElement(list(firstName))
#### Filling in DOB ####
dobSplit <- strsplit(dateOfBirth, split = "")[[1]]
dobNode <- paste0(parentNode, "dateOfBirthControl_DateTextBox")
element <- remoteDriver$findElement(using = "css selector",
dobNode)
element$clearElement()
element$clickElement()
for (i in 1:5) { element$sendKeysToElement(list("\uE012")) }
for (i in seq_along(dobSplit)) { element$sendKeysToElement(list((dobSplit[i]))) }
#### Pressing "Search" Button and Getting Results ####
searchNode <- paste0(parentNode, "searchCommandControl")
searchResults <- getSearchResults(remoteDriver,
pageElement = searchNode,
lastName = lastName,
firstName = firstName,
dateOfBirth = dateOfBirth)
if (all(grepl("\\/", searchResults$dateOfBirth))) {
searchPagesNode <- paste0(parentNode, "searchResultsGridControl_casePager")
searchPages <- xml2::read_html(remoteDriver$getPageSource()[[1]]) %>%
rvest::html_nodes(searchPagesNode) %>%
rvest::html_children() %>%
rvest::html_text()
searchPages <- stringr::str_extract_all(searchPages, "[2-9]*")
if (length(searchPages) > 0) {
searchPages <- searchPages[[1]][searchPages[[1]]!=""]
hasMorePages <- !is.na(searchPages)[1]
} else {
hasMorePages <- F
}
if (hasMorePages) {
secondaryPages <- dplyr::data_frame(
pageNumber = 1:length(searchPages) + 1,
pageElement = paste0(searchPagesNode,
" > div > a:nth-child(",
pageNumber + 2, ")"))
secondaryPages <- secondaryPages[,2]
secondaryResults <- purrr::pmap(secondaryPages,
~ getSearchResults(
remoteDriver = remoteDriver,
pageElement = .,
lastName = lastName,
firstName = firstName,
dateOfBirth = dateOfBirth)) %>% dplyr::bind_rows()
searchResults <- dplyr::bind_rows(searchResults, secondaryResults)
}
#results were returned, so flag accordingly
baseURL <- "https://ujsportal.pacourts.us/DocketSheets/CPReport.ashx?docketNumber="
searchResults <- searchResults %>%
dplyr::mutate(resultReturned = 1,
docketURL = paste0(baseURL, docketNumber))
} else {
#no results were returned, so flag accordingly
searchResults <- searchResults %>%
dplyr::mutate(resultReturned = 0,
docketURL = NA_character_)
}
searchResults$id <- id
print(sprintf("Returned %.0f search results",
nrow(searchResults[which(searchResults$resultReturned == 1),])))
print("=============================")
return(searchResults)
}
# Downloads PDFs based on the output of 'getDocketURLs()'
downloadDockets <- function(searchResults, downloadFolderPath) {
for (i in 1:nrow(searchResults)) {
if (searchResults$resultReturned[i] == 1) {
fileName <- toupper(searchResults$party[1])
fileName <- strsplit(fileName, "\\,")
fileName <- lapply(fileName, function(x) gsub("\\s", "_", x, perl = T))
fileName <- lapply(fileName, function(x) gsub("\\W", "", x, perl = T))
fileName <- paste0(fileName[[1]][1], fileName[[1]][2], "_", i, ".pdf")
download.file(searchResults$docketURL[i],
destfile = file.path(paste0(downloadFolderPath, fileName)),
mode = 'wb')
}
}
}
downloadDocket <- function(docketURL, id, rowNum, downloadFolderPath) {
fileName <- paste0(id, "_", rowNum, ".pdf")
download.file(docketURL,
destfile = file.path(paste0(downloadFolderPath, fileName)),
mode = 'wb')
}
# Cleaning Search Results Table
cleanScrapedTable <- function(searchResults) {
nthRow <- 7
searchResults <- searchResults[seq(1, nrow(searchResults), nthRow), ]
searchResults <- searchResults[,8:17]
names(searchResults) <- c("docketNumber", "shortCaption", "filingDate",
"county", "party", "caseStatus", "OTN", "LOTN",
"policeIncident_complaintNumber",
"dateOfBirth")
rownames(searchResults) <- 1:nrow(searchResults)
searchResults <- searchResults %>% dplyr::mutate_at(vars(colnames(searchResults)),
funs(as.character(.)))
return(searchResults)
}
# Getting Results from Search
getSearchResults <- function(remoteDriver, pageElement, lastName, firstName, dateOfBirth) {
tableXPath <- '//*[@id="ctl00_ctl00_ctl00_cphMain_cphDynamicContent_cphDynamicContent_participantCriteriaControl_searchResultsGridControl_resultsPanel"]/table'
remoteDriver$findElement(using = "css selector", pageElement)$clickElement()
Sys.sleep(1)
searchResultsPage <- xml2::read_html(remoteDriver$getPageSource()[[1]])
noResultsPane <- xml2::xml_find_all(searchResultsPage, xpath = "//*[@id='ctl00_ctl00_ctl00_cphMain_cphDynamicContent_cphDynamicContent_participantCriteriaControl_searchResultsGridControl_noResultsPanel']/table/tbody/tr/td")
searchResults <- data.frame()
if (length(noResultsPane) == 0) {
searchResults <- searchResultsPage %>%
rvest::html_node(xpath = tableXPath) %>%
rvest::html_table(fill = T)
searchResults <- cleanScrapedTable(searchResults)
} else {
searchResults <- data.frame(matrix(nrow = 1, ncol = 10))
names(searchResults) = c("docketNumber", "shortCaption", "filingDate",
"county", "party", "caseStatus", "OTN", "LOTN",
"policeIncident_complaintNumber",
"dateOfBirth")
searchResults[1,] <- c(NA, NA, NA, NA,
paste0(lastName, ", ", firstName),
NA, NA, NA, NA, dateOfBirth)
}
return(searchResults)
}