我怎样才能刮掉这个食谱?
How can I scrape this recipe?
我正在尝试为我自己的个人 collection 抓取一些食谱。它在某些网站上效果很好,因为网站结构有时很容易允许抓取,但有些更难。这个我不知道怎么处理:
https://www.koket.se/halloumigryta-med-tomat-linser-och-chili
现在,假设我想要左边的配料。如果我检查该网站,它看起来像我想要的是两个 article class="ingredients"
块。但我似乎无法到达那里。
我从以下开始:
library(rvest)
library(tidyverse)
read_html("https://www.koket.se/halloumigryta-med-tomat-linser-och-chili") %>%
html_nodes(".recipe-column-wrapper") %>%
html_nodes(xpath = '//*[@id="react-recipe-page"]')
但是,运行 上面的代码显示所有成分都存储在 data-item
中,如下所示:
<div id="react-recipe-page" data-item="{
"chefNames":"<a href='/kockar/siri-barje'>Siri Barje</a>",
"groupedIngredients":[{
"header":"Kokosris",
"ingredients":[{
"name":"basmatiris","unit":"dl","amount":"3","amount_info":{"from":3},"main":false,"ingredient":true
}
<<<and so on>>>
所以我有点疑惑,因为从检查网站来看,一切似乎都整齐地放在我可以提取的东西中,但现在不是了。相反,我需要一些严肃的正则表达式才能得到我想要的一切。
所以我的问题是:我错过了什么吗?有什么方法可以获取 ingredients
篇文章的内容吗?
(我尝试了 SelectorGadget,但它只给了我 No valid path found
)。
您可以使用 rvest
包中的 html_attr("data-item")
提取属性。
此外,data-item 属性看起来像是在 JSON 中,您可以使用 jsonlite
包中的 fromJSON
将其转换为列表:
html <- read_html("https://www.koket.se/halloumigryta-med-tomat-linser-och-chili") %>%
html_nodes(".recipe-column-wrapper") %>%
html_nodes(xpath = '//*[@id="react-recipe-page"]')
recipe <- html %>% html_attr("data-item") %>%
fromJSON
最后,recipe
列表包含许多不相关的不同值,但元素 recipe$ingredients
中也有成分和测量值。
我正在尝试为我自己的个人 collection 抓取一些食谱。它在某些网站上效果很好,因为网站结构有时很容易允许抓取,但有些更难。这个我不知道怎么处理:
https://www.koket.se/halloumigryta-med-tomat-linser-och-chili
现在,假设我想要左边的配料。如果我检查该网站,它看起来像我想要的是两个 article class="ingredients"
块。但我似乎无法到达那里。
我从以下开始:
library(rvest)
library(tidyverse)
read_html("https://www.koket.se/halloumigryta-med-tomat-linser-och-chili") %>%
html_nodes(".recipe-column-wrapper") %>%
html_nodes(xpath = '//*[@id="react-recipe-page"]')
但是,运行 上面的代码显示所有成分都存储在 data-item
中,如下所示:
<div id="react-recipe-page" data-item="{
"chefNames":"<a href='/kockar/siri-barje'>Siri Barje</a>",
"groupedIngredients":[{
"header":"Kokosris",
"ingredients":[{
"name":"basmatiris","unit":"dl","amount":"3","amount_info":{"from":3},"main":false,"ingredient":true
}
<<<and so on>>>
所以我有点疑惑,因为从检查网站来看,一切似乎都整齐地放在我可以提取的东西中,但现在不是了。相反,我需要一些严肃的正则表达式才能得到我想要的一切。
所以我的问题是:我错过了什么吗?有什么方法可以获取 ingredients
篇文章的内容吗?
(我尝试了 SelectorGadget,但它只给了我 No valid path found
)。
您可以使用 rvest
包中的 html_attr("data-item")
提取属性。
此外,data-item 属性看起来像是在 JSON 中,您可以使用 jsonlite
包中的 fromJSON
将其转换为列表:
html <- read_html("https://www.koket.se/halloumigryta-med-tomat-linser-och-chili") %>%
html_nodes(".recipe-column-wrapper") %>%
html_nodes(xpath = '//*[@id="react-recipe-page"]')
recipe <- html %>% html_attr("data-item") %>%
fromJSON
最后,recipe
列表包含许多不相关的不同值,但元素 recipe$ingredients
中也有成分和测量值。