如何让R程序暂停并读取控制台输入?爬虫项目场景适配
Solution for Pausing R Program to Get User Input for Your Scraper
Hey there! I see you're building a recruitment site scraper and need to pause the program to collect two key inputs: the number of profiles to extract and the target position. Let's get this sorted out smoothly.
First, Let's Break Down the Core Issue
You tried readLines and scan, but the main gap is collecting two distinct, valid inputs and making sure the program waits until the user provides them. Your current FUN3 only captures one value, so we'll adjust that to handle both parameters properly.
Step-by-Step Fix
- Use
readline()for each input: This is the simplest, most reliable way to pause R for console input—it won't proceed until the user enters a value and hits Enter. - Add basic input validation: Ensure the number of profiles is a positive integer (since you can't scrape a negative or non-integer number of profiles) and the target position isn't empty.
- Clean up global assignments: Instead of using
<<-to modify global variables, we'll return inputs from a function and assign them directly—this keeps code cleaner and avoids unintended side effects.
Modified Code with Input Handling
Here's how to integrate this into your existing script:
library("dplyr") # Pipe operator library("purrr") # compact library("rvest") # Static webscraping library("RSelenium") # Dynamic webscraping library("stringr") # str_trim library("zeallot") # Multiple assignment with %<-% # -------------------------- # User Input Collection # -------------------------- get_scraper_params <- function() { # Get number of profiles, validate it's a positive integer num_profiles <- NA while(is.na(num_profiles) || num_profiles <= 0) { input <- readline(prompt = "Enter the number of profiles to extract: ") num_profiles <- as.integer(input) if(is.na(num_profiles) || num_profiles <= 0) { cat("Oops, please enter a valid positive integer!\n") } } # Get target position, ensure it's not empty target_position <- "" while(str_trim(target_position) == "") { target_position <- readline(prompt = "Enter the target position to scrape: ") if(str_trim(target_position) == "") { cat("Please enter a non-empty target position!\n") } } # Return both parameters as a list return(list(num_profiles = num_profiles, target_position = target_position)) } # Pause program and collect user inputs scraper_params <- get_scraper_params() num_profiles <- scraper_params$num_profiles target_position <- scraper_params$target_position # Confirm inputs with the user cat(paste0("\nGot it! You want to extract ", num_profiles, " profiles for the position: ", target_position, "\n")) # -------------------------- # Global Settings & Initialization # -------------------------- options(stringsAsFactors = FALSE) options(encoding="utf-8") # Initialize empty lists for data storage vector <- c("name","ind","post","locate","currently_With","role_Hiring") c(name, ind, position, locate, currently_With, role_Hiring) %<-% lapply(vector, function(x) list()) # Start Selenium server rD <- rsDriver(port = 4547L, browser="firefox") remDr <- rD[["client"]] # -------------------------- # Your Scraper Logic Here # -------------------------- # Use num_profiles and target_position in your scraping functions # ...
Quick Notes
- RStudio vs. Console: If you're running this in RStudio, input prompts will show up in the Console pane (not the script editor)—make sure you look there to enter your values.
- Validation: The loops ensure you don't get invalid data (like a negative number of profiles) that could break your scraper later.
内容的提问来源于stack exchange,提问作者user11856746
相关产品推荐
相关产品推荐

