我正在学习rvest包
我能够将列表项的链接 URL 抓取为单个项目
website <- "https://www.imdb.com/search/title/?title_type=feature&genres=crime"
page <- read_html(website)
xpath_expression <- '/html/body/div[2]/main/div[2]/div[3]/section/section/div/section/section/div[2]/div/section/div[2]/div[2]/ul/li[1]/div[1]/div/div/div[1]/div[2]/div[1]/a'
link_url <- html_attr(html_node(page, xpath = xpath_expression), "href")
但是,如果我想从列表的第一部分中抓取所有 50 个链接 URL,它只会返回“字符空。我尝试通过 xpath 识别它们并从那里提取它
website <- "https://www.imdb.com/search/title/?title_type=feature&genres=crime"
page <- read_html(website)
xpath_expression <- '//*[@id="main"]/div/div[3]/div[3]/div/div/div[1]/div[3]/h3/a'
link_nodes <- html_nodes(page, xpath = xpath_expression)
链接网址<- html_attr(link_nodes, "href")
[3]
作为谓词,您将告诉 XPath 引擎仅使用具有 div
的 position()=3
。因此可能适用于 1 个项目,但不适用于所有项目。尝试以下 xpath:
//li[@class='ipc-metadata-list-summary-item']//a[@class='ipc-title-link-wrapper']
使用的类可能不会改变