How do you identify CSS Selectors when inspecting HTML code? - html

I'm trying to web scrape housing data using the rvest package, but I'm having difficulty identifying html nodes. My (generic) R code is as follows:
housing_wp <- read_html("webpage")
address <- housing_wp %>%
html_nodes("a.unhideListingLink") %>%
html_text()
address
The html code inspected is in the following link that contains the data I am trying to input into a table in R. What am I doing wrong?
HTML

Related

How to extract the table for each subject from the URL link in R - Webscraping

I'm trying to scrape the table for each subject:
This is the main link https://htmlaccess.louisville.edu/classSchedule/setupSearchClassSchedule.cfm?error=0 It looks like below:
I have to select each subject and click search, which takes to the link https://htmlaccess.louisville.edu/classSchedule/searchClassSchedule.cfm
Each subject gives a different table. For subject Accounting I tried to get the table like below: I used Selector Gadget Chrome extension to get the node string for html_nodes
library(rvest)
library(tidyr)
library(dplyr)
library(ggplot2)
url <- "https://htmlaccess.louisville.edu/classSchedule/searchClassSchedule.cfm"
df <- read_html(url)
tot <- df %>%
html_nodes('table+ table td') %>%
html_text()
But it didn't work:
## show
tot
character(0)
Is there a way to get the tables for each subject in a code with R?
Your problem is that the site requires a web form be submitted - that's what happens when you click the "Search" button on the page. Without submitting that form, you won't be able to access the data. This is evident if you attempt to navigate to the link you're trying to scrape - punch that into your favorite web browser and you'll see that there's no tables at all at "https://htmlaccess.louisville.edu/classSchedule/searchClassSchedule.cfm". No wonder nothing shows up!
Fortunately, you can submit web forms with R. It requires a little bit more code, however. My favorite package for this is httr, which partners nicely with rvest. Here's the code that will submit a form using httr and then proceed with the rest of your code.
library(rvest)
library(dplyr)
library(httr)
request_body <- list(
term="4212",
subject="ACCT",
catalognbr="",
session="none",
genEdCat="none",
writingReq="none",
comBaseCat="none",
sustainCat="none",
starttimedir="0",
starttimehour="08",
startTimeMinute="00",
endTimeDir="0",
endTimeHour="22",
endTimeMinute="00",
location="any",
classstatus="0",
Search="Search"
)
resp <- httr::POST(
url = paste0("https://htmlaccess.louisville.edu/class",
"Schedule/searchClassSchedule.cfm"),
encode = "form",
body = request_body)
httr::status_code(resp)
df <- httr::content(resp)
tot <- df %>%
html_nodes("table+ table td") %>%
html_text() %>%
matrix(ncol=17, byrow=TRUE)
On my machine, that returns a nicely formatted matrix with the expected data. Now, the challenge was figuring out what the heck to put in the request body. For this, I use Chrome's "inspect" tool (right click on a webpage, hit "inspect"). On the "Network" tab of that side panel, you can track what information is being sent by your browser. If I start on the main page and keep that side tab up while I "search" for accounting, I see that the top hit is "searchClassSchedule.cfm" and open that up by clicking on it. There, you can see all the form fields that were submitted to the server and I simply copied those over into R manually.
Your job will be to figure out what shortened name the rest of the departments use! "ACCT" seems to be the one for "Accounting". Once you've got those names in a vector you can loop over them with a for loop or lapply statement:
dept_abbrevs <- c("ACCT", "AIRS")
lapply(dept_abbrevs, function(abbrev){
...code from above...
...after defining message body...
message_body$subject <- abbrev
...rest of the code...
}

RVest html_nodes() function not recognizing web element

I an attempt to run the following code to scrape the table titled "Advanced", whos id is also dictated as "advanced":
url <- paste0("https://www.basketball-reference.com/teams/GSW/2016.html")
webpage <- read_html(url)
col_names <- webpage %>%
html_nodes("table#advanced")
However, when I run this code and try to print col_names, I receive the following value: {xml_nodeset (0)}. How can I properly scrape my desired table?

Scraping Weblinks out of an Website using Rvest

im new to r and Webscraping. I'm currently scraping a realestate website (https://www.immobilienscout24.de/Suche/S-T/Wohnung-Miete/Rheinland-Pfalz/Koblenz?enteredFrom=one_step_search) but i don't manage to scrape the links of the specific offers.
When using the code below, i get every link attached to the Website, and im not quite sure how i can filter it in a way that it only scrapes the links of the 20 estate offers. Maybe you can help me.
Viewing the source code / inspecting the elements didn't help me so far...
url <- immo_webp %>%
html_nodes("a") %>%
html_attr("href")
You can target the article tags and then construct the urls from the data-obid attribute by concatenating with a base string
library(rvest)
library(magrittr)
base = 'https://www.immobilienscout24.de/expose/'
urls <- lapply(read_html("https://www.immobilienscout24.de/Suche/S-T/Wohnung-Miete/Rheinland-Pfalz/Koblenz?enteredFrom=one_step_search")%>%
html_nodes('article')%>%
html_attr('data-obid'), function (url){paste0(base, url)})
print(urls)

Nothing Returning When Using rvest and xpath on an html page

i'm using xpath and rvest for scraping an htm page. Other examples of rvest work well with pipelines, but for this particular script nothing is returned.
webpage <- read_html("https://www.sec.gov/litigation/admin/34-45135.htm")
whomst <- webpage %>% html_nodes(xpath = '/html/body/table[2]/tbody/tr/td[3]/font/p[1]/table/tbody/tr/td[1]/p[2]')
What is returned is :
{xml_nodeset (0)}
Here is a screenshot of the page and the corresponding html
And here's the page that I'm on: https://www.sec.gov/litigation/admin/34-45135.htm. I'm trying to extract the words, "PINNACLE HOLDINGS, INC."
Sometimes chrome tool doesn't give accurate xpath or css, you need to try by yourself, this selector works:
webpage %>% html_nodes("td > p:nth-child(3)") %>% html_text()
result:
[1] "PINNACLE HOLDINGS, INC., \n

Web scraping using R - Table of many pages

I have this website which has a table of many pages. Can someone help me read all pages of that table into R?
Website:
https://www.fdic.gov/bank/individual/failed/banklist.html
You can scrape the entire HTML table using the rvest package. See the code below. The code automatically identifies the entire table and reads in all 555 entries.
require(rvest)
URL <- "https://www.fdic.gov/bank/individual/failed/banklist.html"
failed_banks <- URL %>%
read_html() %>%
html_table() %>%
as.data.frame()