FactCheck.org SciCheck Articles

Dataset
Scraping
FactCheck.org’s SciCheck Articles
Author

Jaden Chant

Link to FactCheck.org SciCheck Articles

Link to FactCheck.org SciCheck Articles Data

Get all article data

get_article_data <- function(url) {
  print(url)
    
  robotstxt::paths_allowed(url)
  
  Sys.sleep(2)
  
  html <- tryCatch({
    read_html(url)
  }, error = function(e) {
    print("Error with read_html, trying read_html_live")
    print(e)
    tryCatch({
      read_html_live(url)
    }, error = function(e) {
      print("Error with read_html_live")
      print(e)
      return(NA)
    })
  })
  
  # Header metadata
  header <- html |> html_elements("header")
  
  title <- header |> html_elements("h1") |> html_text()
  
  subheader <- header |> html_elements("p")
  
  author <- subheader[1] |> html_elements("a") |> html_text() |> paste(collapse = ", ")
  
  date <- subheader[2] |> html_elements("time") |> html_text()
  
  # Footer Metadata
  footer <- html |> html_elements("article") |> html_elements("footer")
  
  categories <- footer |> html_elements(".categories") |> html_elements("li") |> html_text2() |> paste(collapse = ", ")
  
  tags <- footer |> html_elements(".post_tag") |> html_elements("li") |> html_text2() |> paste(collapse = ", ")
  
  issue <- footer |> html_elements(".issue") |> html_elements("li") |> html_text2() |> paste(collapse = ", ")
  
  people <- footer |> html_elements(".person") |> html_elements("li") |> html_text2() |> paste(collapse = ", ")
  
  misconceptions <- footer |> html_elements(".misconceptions") |> html_elements("li") |> html_text2() |> paste(collapse = ", ")
  
  location <- footer |> html_elements(".location") |> html_elements("li") |> html_text2() |> paste(collapse = ", ")
  
  # Article
  article <- html |> 
  html_elements("article") |>
  html_elements(".entry-content") |>
  html_text2()

  # Remove Spanish translation option
  em <- html |> 
    html_elements("article") |>
    html_elements(".entry-content") |>
    html_elements("em") |>
    html_text2() |>
    first()
  
  if(!is.na(em) && str_detect(article, "español")) {
    article <- article |> 
      str_remove(em)
  }
  
  row <- tibble(
    title = as.character(title),
    author = as.character(author),
    date = as.character(date),
    url = as.character(url),
    categories = as.character(ifelse(categories == "", NA, categories)),
    tags = as.character(ifelse(tags == "", NA, tags)),
    issue = as.character(ifelse(issue == "", NA, issue)),
    people = as.character(ifelse(people == "", NA, people)),
    misconceptions = as.character(ifelse(misconceptions == "", NA, misconceptions)),
    location = as.character(ifelse(location == "", NA, location)),
    article = as.character(article))
  
  return(row)
}
links <- read_csv("./pages/projects/scicheck/data/factcheck_scicheck_links.csv")

split_links <- split(links, cut(seq(nrow(links)), breaks = 6, labels = FALSE))

for(i in 1:length(split_links)) {
  scicheck <- tibble() |>
    bind_rows(lapply(split_links[[i]]$link, get_article_data))
  
  scicheck |> write_csv(str_c("./pages/projects/scicheck/data/factcheck_scicheck_", i, ".csv"))
}

scicheck1 <- read_csv("./pages/projects/scicheck/data/factcheck_scicheck_1.csv")
scicheck2 <- read_csv("./pages/projects/scicheck/data/factcheck_scicheck_2.csv")
scicheck3 <- read_csv("./pages/projects/scicheck/data/factcheck_scicheck_3.csv")
scicheck4 <- read_csv("./pages/projects/scicheck/data/factcheck_scicheck_4.csv")
scicheck5 <- read_csv("./pages/projects/scicheck/data/factcheck_scicheck_5.csv")
scicheck6 <- read_csv("./pages/projects/scicheck/data/factcheck_scicheck_6.csv")

scicheck <- tibble() |>
  bind_rows(scicheck1, scicheck2, scicheck3, scicheck4, scicheck5, scicheck6) |>
  mutate(date = as.Date(date, format = "%B %d, %Y"))

scicheck |> write_csv("./data/factcheck_scicheck.csv")
scicheck <- read_csv("./data/factcheck_scicheck.csv")

added_links <- tibble(link = get_links(1)) |>
  select(link) |>
  filter(!link %in% scicheck$url)

new_scicheck <- tibble() |>
  bind_rows(lapply(added_links$link, get_article_data)) |>
  mutate(date = as.Date(date, format = "%B %d, %Y"))

combined_scicheck <- scicheck |> bind_rows(new_scicheck)

combined_scicheck |> arrange(date) |> write_csv("./data/factcheck_scicheck.csv")