使用RSelenium获取库存时无法保存响应的技术求助
Hey there! I see you've already got the click action working to reveal inventory info on Vinmonopolet's product page—nice progress! Here's how you can capture that rendered response and extract the inventory data you need:
Step 1: Capture the Full Rendered Page Source
After clicking the element that loads the inventory, you can grab the entire updated HTML of the page using RSelenium's getPageSource() method. This will capture all dynamically rendered content as a string, which you can store in a variable or even save to a file.
Step 2: Parse the HTML to Extract Inventory Details
To make sense of the raw HTML, use the rvest package (a staple for web scraping in R) to parse the content and pull out the specific inventory information you're targeting. This is way cleaner than working with raw HTML strings directly.
Full Working Code Example
# Load required libraries library(RSelenium) library(rvest) # Initialize RSelenium driver rD <- rsDriver() remDr <- rD[["client"]] # Navigate to the product page remDr$navigate("https://www.vinmonopolet.no/vmp/Land/Chile/Gato-Negro-Cabernet-Sauvignon-2017/p/295301") # Click the element to reveal inventory webElement <- remDr$findElement('xpath', '//*[@id="product_2953010"]/span[2]') webElement$clickElement() # Wait for the page to fully render (adjust time if your connection is slow) Sys.sleep(2) # Capture the rendered page source ([[1]] pulls the string from the returned list) page_source <- remDr$getPageSource()[[1]] # Optional: Save the page source to a file for debugging write(page_source, "vinmonopolet_inventory.html") # Parse the HTML with rvest parsed_html <- read_html(page_source) # Extract inventory data (update the selector to match the actual inventory element) # Use your browser's dev tools (F12) to copy the correct CSS/XPath for the inventory text inventory_data <- parsed_html %>% html_element(css = ".some-inventory-class") %>% # Replace with real selector html_text(trim = TRUE) # Print or store your inventory info cat("Current inventory status:", inventory_data, "\n") # Clean up: Close the driver properly remDr$close() rD$server$stop()
Key Tips:
- Wait for Rendering: The
Sys.sleep(2)gives the page time to load dynamic inventory content. For more reliability, replace it with RSelenium'swaitForElementmethod to wait until the inventory element is visible, instead of using a fixed sleep time. - Adjust Selectors: Inspect the rendered page with your browser's dev tools to find the exact CSS or XPath selector for the inventory text. Right-click the inventory element and copy its selector to use in
html_element(). - Save for Debugging: Saving the page source to a file lets you open it in a browser and confirm that all the inventory content was captured correctly.
内容的提问来源于stack exchange,提问作者Patricio Lobos

