R code for extracting data without using CSS path

Hey there, I'm reaching out because I'm struggling to figure out how to extract data from a webpage (""). Learning how to scrape data is my current project, and I'm specifically trying to gather contact information (Office, Fax, email) from the website mentioned. Unfortunately, I'm having difficulty identifying a specific CSS path using Selectorgadget in R, and the script I'm using looks something like this:

library(rvest)
page_name <- read_html("page html")

page_name %>%
html_node("selector gadget node") %>%
html_text()

I managed to scrape all the other data successfully, but I'm stuck on extracting the contact information. Any help or guidance would be greatly appreciated, as my head is spinning trying to figure this out. Thanks in advance!

Answer №1

The solution is right in front of us. Each contact block is equipped with a .council-list list class, allowing for easy extraction of contact information. Utilize string/regex operations to isolate the specific fields.

library(rvest)
page_content <- read_html('https://nabtu.org/about-nabtu/official-directory/building-trades-local-councils-overview/')
contact_info = page_content %>%
  html_nodes('.council-list') %>%
  html_text()

# Filter out irrelevant strings
contact_info = grep(x = contact_info, 'Email|Fax|office', ignore.case = T, value = T)

# Extract relevant information 
library(stringr)
library(magrittr)
office_number = str_extract(contact_info, 'Office:[^[:alpha:]]*')
fax_number = str_extract(contact_info, 'Fax:[^[:alpha:]]*')
email_address = str_extract(contact_info, 'Email: [^ ]*')

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Change the appearance of text with a button click using JavaScript

Currently mastering JavaScript but encountering an issue. How do I change the text to underline when clicking on "Click Me"? <p id="demo" style=" text-decoration:none; ">Hello JavaScript!</p> <button type="button" onclick="document.getE ...

Creating a custom design for a button using an external CSS file

I have an HTML code linked to a CSS file where I'd like to style buttons. The button styles are defined in the CSS file. Everything else is working fine, but when I attempt to apply the styled button, it reverts back to a standard HTML button unless ...

What are some strategies for customizing the appearance of child components within a parent component?

I have a scenario where I am using parent and child components. When I use the HTML in another component, I also apply my CSS. For example, in my parent component: HTML <div class="chips"> <p class="tags">Tag 1</p&g ...

SendFileS (Part 2): Emailing Documents as Attachments

After successfully sending html-formatted email messages by following the guidance in this related question, I am now looking to enhance the code to include file attachments of any type. How can I adjust the code below to accomplish this? library(sendmai ...

Is there a way in R to generate a condensed data frame containing only the variables utilized in a statistical model?

Consider the following scenario: fit <- lm(Sepal.Length ~ log(Sepal.Width), data = iris) I am in need of a modified version of iris dataset that contains only the variables used in creating fit. Using model.matrix() or model.frame() is not sufficient b ...

Guide: Using jQueryUI's explode effect to animate an HTML element explosion

I'm having trouble getting the jQueryUI explode effect to work properly. I've tested it out on this jsfiddle, but the explosion effect doesn't seem to happen as expected - the element just disappears with no explosion animation. $('h1, ...

Performing addition operations with Rcpp's IntegerVectors

Here's an issue I've encountered across different scenarios. Essentially, I am faced with an C++ int and an Rcpp IntegerVector, and my goal is to add one integer to another and store the result in a new IntegerVector. While this problem also aris ...

Difficulty encountered in aligning items within neighboring table cells vertically

Currently, I am facing a styling issue with a few table rows. In this particular screenshot: https://i.sstatic.net/FcmJW.png The cells in the blue and red circles are part of a table row (with a height of 50px). Both are set to "vertical-align:top". The ...

Tips for avoiding stacked bars in the presence of repeating factors

When plotting a barplot, I'm struggling with preventing the same factor from stacking on top of itself. The issue arises because I end up with multiple repetitions of the factor in the plot. Here's an example to illustrate: library("ggplot2") my ...

Exploring ways to interact with Span elements using Selenium in Python

Greetings! I am currently attempting to access a specific element called span. My goal is to retrieve this element in a manner that allows me to append it to a list. Each span contains an attribute wordnr="1" or a different number. The objective is to extr ...

What is the method to reduce the gap between the label and field in Vuetify input controls?

Consider a custom Vuetify text field as an example (Playground) <template> <v-app> <v-container> <v-text-field label="label goes here" variant="plain" density="compact" ...

Calculating the difference in days between two dates for any day-count basis using R

Is there a specific function available in R that can accurately determine the number of days between two dates using various day-count methods? I am in search of similar functionality to Matlab's daysdif function. Specifically, my focus is on calcula ...

Steps for incorporating a storyline into a CSS Chart

I'm attempting to introduce a horizontal line at a specific point using chartscss.org For instance, on the given chart utilizing Charts CSS below, I'm aiming to include a horizontal line at 15.5... Similar to the illustration shown. I've e ...

CSS animations - transitioning opacity in and out

Looking to add some CSS animations to a menu but running into some issues. I've got the code below for the menu to fade in on hover, but can't quite figure out how to make it fade out smoothly as well. I've tried a few approaches, but all I ...

What is the best way to add an ellipsis to the conclusion of a paragraph using .dotdotdot?

I'm struggling with implementing the ellipsis function on my website. My goal is to have the ellipsis appear at the end of each paragraph in the news_inner div (morgan, pia, and gold). I found the function on , but I'm having difficulty understan ...

Tips on how to change the color scheme of a webpage

I am currently working with basic HTML and CSS, but I am facing an issue where my background color is only filling a third of the page instead of the entire page. Despite attempting to use the html and body classes for the background color, it did not prod ...

What is the method for altering the text color within a disabled input field?

Currently facing difficulty in changing the color of the text within the input field when it's disabled. I'm utilizing Material UI for this purpose. import React from "react"; import { makeStyles } from "@material-ui/core/st ...

When interacting with elements with the "user-select: none" property, WkWebView still allows text selection

When using an iOS iPad app with a single fullscreen WKWebView, an issue arises when long-pressing on an area with user-select:none - the first text in the next available area without user-select:none gets selected instead of being ignored. Is there a way t ...

Using Bootstrap4 to merge rows into a single column or apply rowspan in Bootstrap

Hey there, I have a specific requirement that I need help with. Check out the image here. I want to enable the LCM information box when the LCM checkbox is checked. Below is my code: <div class="panel-body "> <div class="c ...

Challenges with text in a Bootstrap 5 responsive carousel

I'm currently in the process of developing a new website and I am utilizing bootstrap 5. One issue I have encountered is that the text and button within the carousel in the header section do not appear to be compatible. To provide better clarity, I wi ...