You're an helpful programmer assistant, working with a human user developing a web…
- User
You're an helpful programmer assistant, working with a human user developing a web scrapper for a particular website in RStudio using rvest package. Ask any clarifying questions before proceeding if needed. Avoid writing all the code, and try to focus on what needs to be changed or added.
I need to get multiple variables from that "table" (table#subjectDispositionSection.sectionTable) The first part of the data is in tabular form, with only two columns: first column with the title of the variable (class="labelColumn"), and second column is the actual value (class="valueColumn"). The second part of the data is in a tabular form with a variable number of columns (class="embeddedTableContainer"). We're focusing on the first part for now.
Here's the code so far:
library(rvest)
url <- "https://www.clinicaltrialsregister.eu/ctr-search/trial/2016-000172-19/results" # replace with the URL of the website you want to scrape webpage <- read_html(url)
Extract the data using the provided CSS selector and classes:
table_selector <- "table#subjectDispositionSection.sectionTable" label_selector <- ".labelColumn" value_selector <- ".valueColumn"
label_nodes <- html_nodes(webpage, paste(table_selector, label_selector, sep = " ")) value_nodes <- lapply(label_nodes, function(node) { html_node(node, xpath = "following-sibling::*[1][contains(@class, 'valueColumn')]") })
labels <- trimws(html_text(label_nodes)) values <- trimws(sapply(value_nodes, html_text))
Combine the extracted labels and values into a data frame:
data <- data.frame(Variable = labels, Value = values, stringsAsFactors = FALSE)
THe problem is that the code so far is extracting the second part as well, but it is intended to get the first part only. It should ignore any data inside those embedded tables (class="embeddedTableContainer"). Here's the HTML code for one of those embedded tables to be ignored, in case it is helpful:
<tbody><tr> <td class="header labelColumn"> <div>Number of subjects in period 1 </div> </td> <td class="valueColumn centeredValueColumn">Empagliflozin</td> <td class="valueColumn centeredValueColumn">Placebo</td> </tr> <tr> <td class="labelColumn"> <div> Started</div> </td> <td class="valueColumn centeredValueColumn"><div>22</div></td> <td class="valueColumn centeredValueColumn"><div>22</div></td> </tr> <tr> <td class="labelColumn"> <div> Completed</div> </td> <td class="valueColumn centeredValueColumn"><div>22</div></td> <td class="valueColumn centeredValueColumn"><div>22</div></td> </tr> </tbody></table> </div> </td> </tr> - Assistant
To avoid extracting the second part of the data, you can add a condition to exclude the nodes that are ancestors of the "embeddedTableContainer" class.
First, you need to update the
label_nodesby adding a condition[not(ancestor::td[contains(@class, 'embeddedTableContainer')])]to the XPath expression as follows:label_nodes <- html_nodes(webpage, paste(table_selector, label_selector, '[not(ancestor::td[contains(@class, "embeddedTableContainer")])]', sep = " "))Then, replace the
lapplyfunction forvalue_nodeswith afor loopthat iteratively goes through each label_node without extracting the data inside the embedded tables:value_nodes <- vector("list", length = length(label_nodes)) for (i in seq_along(label_nodes)) { value_nodes[[i]] <- html_node(label_nodes[[i]], xpath = "following-sibling::*[1][contains(@class, 'valueColumn') and not(ancestor::td[contains(@class, 'embeddedTableContainer')])]") }These changes will exclude the data inside the embedded tables, and the output should contain only the first part of the data.
- User
Error in tokenize(css) : Unexpected character '@' found at position 86
- Assistant
I apologize for the confusion, the problem is due to using the "@" symbol in the CSS selector. We need to convert the CSS selector to an XPath selector to correctly exclude the embedded tables.
- Convert the table,
.labelColumn, and.embeddedTableContainerCSS selectors to their corresponding XPath selectors.
table_selector <- "//table[@id='subjectDispositionSection' and contains(@class, 'sectionTable')]" label_selector <- ".//td[contains(@class, 'labelColumn')]" container_selector <- "ancestor::td[contains(@class, 'embeddedTableContainer')]"- Use the XPath selectors to get only the
labelColumnnodes that don't have ancestors withembeddedTableContainerclass:
label_nodes_xpath <- paste(label_selector, "[not(", container_selector, ")]") label_nodes <- html_nodes(webpage, xpath = paste(table_selector, label_nodes_xpath, sep = ""))- Update the code for getting
value_nodesto use the new XPath selectors:
- Convert the table,
- User
Warning message: In xml_find_all.xml_node(x, make_selector(css, xpath)) : Invalid expression [1207]
- Assistant
I apologize for the confusion caused by the error. It seems that the issue is related to the XPath expression used.
Here is the corrected XPath expression which should resolve the issue:
- First, let's simplify and update the table_selector, label_selector, and container_selector XPath expressions:
table_selector <- "//table[contains(@class, 'sectionTable') and @id='subjectDispositionSection']" label_selector <- ".//td[contains(@class, 'labelColumn')]" container_selector <- "ancestor::td[contains(@class, 'embeddedTableContainer')]"- Use the updated XPath selectors to get only the
labelColumnnodes that don't have ancestors withembeddedTableContainerclass:
label_nodes_xpath <- paste(label_selector, "[not(", container_selector, ")]") label_nodes <- html_nodes(webpage, xpath = paste(table_selector, label_nodes_xpath, sep = ""))- Update the code for getting
value_nodeswhile using the new XPath selectors: