如何使用rvest爬取SVG中的<a>标签?R语言技术求助
Hey, let's break down how to grab those SVG links you're after—your current code only pulls header links because it's not targeting the SVG-specific elements correctly. Here's what you can do:
First, Check the SVG's Structure
Start by right-clicking the map in your browser and inspecting it to figure out how it's loaded:
- If it's an
<svg>tag directly nested in the HTML (the most common setup for interactive maps), we can target it directly. - If it's loaded via
<object>/<embed>or an<img>(note:<img>won't let you scrape internal links), you'll need to first grab the SVG's source URL.
Step 1: Target SVG Links with rvest (Static Content)
If the SVG is embedded in the HTML, adjust your selector to specifically look for links inside the SVG. SVG links might use standard <a> tags, but older SVGs often use xlink:href instead of the regular href attribute. Try this:
library(rvest) # First, handle login with a persistent session (if required) session <- session("https://your-target-login-page.com") # Fill out the login form (replace with your form's actual field names) login_form <- html_form(session)[[1]] filled_form <- set_values(login_form, username = "your-username", password = "your-password") session <- session_submit(session, filled_form) # Navigate to the page with the SVG map map_page <- session %>% session_jump_to("https://your-map-page.com") # Grab <a> tags nested inside the SVG svg_a_tags <- map_page %>% html_nodes("svg a") # Extract links—check both href and xlink:href (SVG-specific attribute) svg_links <- svg_a_tags %>% html_attr("href") %>% # Fall back to xlink:href if href is missing ifelse(is.na(.), html_attr(svg_a_tags, "xlink:href"), .) # Convert relative URLs to absolute ones (critical for usable links) base_url <- "https://your-target-domain.com" absolute_links <- url_absolute(svg_links, base_url)
Step 2: Handle Dynamic SVG Content (JS-Rendered)
If the SVG loads after the initial page load (via JavaScript), rvest won't see it because it only parses static HTML. For this, use a browser automation tool like RSelenium to render the page fully:
Example with RSelenium:
library(RSelenium) library(rvest) # Start a Chrome driver (ensure ChromeDriver is installed and in your system PATH) driver <- rsDriver(browser = "chrome", port = 4567L) remDr <- driver[["client"]] # Navigate to login page and authenticate remDr$navigate("https://your-target-login-page.com") remDr$findElement(using = "id", value = "username-input")$sendKeysToElement(list("your-username")) remDr$findElement(using = "id", value = "password-input")$sendKeysToElement(list("your-password")) remDr$findElement(using = "css selector", value = "button[type='submit']")$clickElement() # Wait for login to complete, then go to the map page Sys.sleep(2) # Adjust wait time based on your page's load speed remDr$navigate("https://your-map-page.com") # Get the fully rendered page source page_source <- remDr$getPageSource()[[1]] map_page <- read_html(page_source) # Extract SVG links like before svg_links <- map_page %>% html_nodes("svg a") %>% html_attr("href") %>% ifelse(is.na(.), html_attr(., "xlink:href"), .) # Clean up the browser session remDr$close() driver$server$stop()
Quick Tips
- Always use your browser's dev tools to confirm what attribute holds the links (it could be
href,xlink:href, or even a custom data attribute likedata-link). - If the SVG is a separate file (e.g.,
<object data="/maps/usa.svg">), grab thatdataURL, use your authenticated session to fetch it, then parse it as XML to extract links.
内容的提问来源于stack exchange,提问作者rotz

