You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让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

  1. 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.
  2. 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.
  3. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 06:35:11