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

从moweek.com.uy抓取VESTIMENTA分类图片至对应子文件夹

问题描述

我需要从https://moweek.com.uy/抓取图片,该网站包含VESTIMENTA、CALZADO、ACCESORIOS等主分类。目标是进入VESTIMENTA分类下的Activewear、Blazers y chaquetas等子分类,将每个子分类中的图片下载到名为images的主文件夹下,以对应子分类名称命名的子文件夹中。此前可用的R语言代码现在出现问题,当前代码如下,图片信息抓取已单独放在另一脚本中,需修复代码实现需求:

library(tidyverse)
library(rvest)
library(httr)

url <- "https://moweek.com.uy/"
html_content <- GET(url)

webpage <- read_html(content(html_content, as = "text"))

category_nodes <- html_nodes(webpage, "a.headerOption.headerLink[data-category-id='1']")
category_urls <- html_attr(category_nodes, "href")

dir.create("images", showWarnings = FALSE)

# Loop through subcategories and download images
for (category_url in category_urls) {
  # Remove any extra slashes in the URL construction
  subcategory_url <- paste0(url, gsub("/+", "/", category_url))
  
  subcategory_page <- tryCatch({
    read_html(subcategory_url)
  }, error = function(e) {
    message(paste("Error accessing URL:", subcategory_url))
    return(NULL)
  })
  
  if (is.null(subcategory_page)) {
    next  # Skip to the next subcategory if an error occurred
  }
  
  subcategory_name <- subcategory_page %>% html_text()
  
  # Clean the subcategory name for folder creation
  subfolder_name <- gsub(" ", "_", tolower(subcategory_name))
  subfolder_path <- file.path("images", subfolder_name)
  dir.create(subfolder_path, showWarnings = FALSE)
  
  # Extract image URLs and download them into the subfolder
  image_urls <- subcategory_page %>% 
    html_nodes(".your-image-selector") %>%
    html_attr("src")
  
  for (image_url in image_urls) {
    image_name <- basename(image_url)
    download.file(image_url, file.path(subfolder_path, image_name))
  }
}
代码问题排查与修复方案

1. 子分类名称提取错误

原代码中subcategory_name <- subcategory_page %>% html_text()会提取整个页面的文本,完全无法得到正确的子分类名称。需要定位到页面中专门显示分类标题的节点,该网站子分类页面的标题在h1.title元素中,修改为:

subcategory_name <- subcategory_page %>% 
  html_node("h1.title") %>% 
  html_text(trim = TRUE)

2. 图片选择器为占位符

原代码里的.your-image-selector是未替换的占位符,该网站商品图片的实际class为product-image img-fluid,替换后:

image_urls <- subcategory_page %>% 
  html_nodes(".product-image.img-fluid") %>%
  html_attr("src") %>%
  na.omit()

3. URL拼接逻辑缺陷

如果category_url本身是完整的绝对路径,原代码的拼接方式会生成错误URL。添加判断逻辑处理绝对/相对路径:

subcategory_url <- if (!str_starts(category_url, "http")) {
  paste0(url, gsub("^/+", "", category_url))
} else {
  category_url
}

4. 图片下载模式错误

download.file默认模式不适合二进制图片,添加mode = "wb"避免图片损坏:

download.file(image_url, file.path(subfolder_path, image_name), mode = "wb")

5. 空URL与反爬处理

添加空URL过滤逻辑,同时增加请求延迟避免被网站拦截:

for (image_url in image_urls) {
  if (image_url == "") next
  image_name <- basename(image_url)
  download.file(image_url, file.path(subfolder_path, image_name), mode = "wb")
  Sys.sleep(0.5)
}
修复后的完整代码
library(tidyverse)
library(rvest)
library(httr)

url <- "https://moweek.com.uy/"
html_content <- GET(url)
webpage <- read_html(content(html_content, as = "text"))

# 定位VESTIMENTA分类下的子分类节点
category_nodes <- html_nodes(webpage, "a.headerOption.headerLink[data-category-id='1']")
category_urls <- html_attr(category_nodes, "href")

dir.create("images", showWarnings = FALSE)

# 遍历子分类下载图片
for (category_url in category_urls) {
  # 处理URL,确保路径正确
  subcategory_url <- if (!str_starts(category_url, "http")) {
    paste0(url, gsub("^/+", "", category_url))
  } else {
    category_url
  }
  
  subcategory_page <- tryCatch({
    read_html(subcategory_url)
  }, error = function(e) {
    message(paste("无法访问URL:", subcategory_url))
    return(NULL)
  })
  
  if (is.null(subcategory_page)) next
  
  # 提取子分类名称
  subcategory_name <- subcategory_page %>% 
    html_node("h1.title") %>% 
    html_text(trim = TRUE)
  
  # 清理名称用于创建文件夹
  subfolder_name <- gsub(" ", "_", tolower(subcategory_name))
  subfolder_path <- file.path("images", subfolder_name)
  dir.create(subfolder_path, showWarnings = FALSE)
  
  # 提取商品图片URL
  image_urls <- subcategory_page %>% 
    html_nodes(".product-image.img-fluid") %>%
    html_attr("src") %>%
    na.omit()
  
  # 下载图片
  for (image_url in image_urls) {
    if (image_url == "") next
    image_name <- basename(image_url)
    download.file(image_url, file.path(subfolder_path, image_name), mode = "wb")
    Sys.sleep(0.5)
  }
}

内容的提问来源于stack exchange,提问作者Paula

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 07:15:04