Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
rvest抓取html内容值_Html_R - Fatal编程技术网

rvest抓取html内容值

rvest抓取html内容值,html,r,Html,R,我正在尝试刮取以下页面:为了创建一个包含6列的数据框架:位置、公司和元1-5。不幸的是,例如,我不知道如何捕捉内容中的值,因此在本例中,在创建我的数据帧时将使用值Tauragė 我的初始代码: if(!require("tidyverse")) install.packages("tidyverse"); library("tidyverse") if(!require("rvest")) install.packa

我正在尝试刮取以下页面:为了创建一个包含6列的数据框架:位置、公司和元1-5。不幸的是,例如,我不知道如何捕捉内容中的值,因此在本例中,在创建我的数据帧时将使用值Tauragė

我的初始代码:

if(!require("tidyverse")) install.packages("tidyverse"); library("tidyverse")
if(!require("rvest")) install.packages("rvest"); library("rvest")

# setting url and reading html code 
url <- "https://www.cv.lt/employee/announcementsAll.do?regular=true&salaryInterval=-1&interval=2&ipp=1000"
html <- read_html(url, encoding = "utf-8")

# creating a dataframe of ads
ads <- html %>%{
  data.frame(
    position=html_nodes(html, "tbody p a:nth-child(1)") %>% html_text(),
    company=html_nodes(html, "tbody p a:nth-child(2)")%>% html_text(),
    meta1=...
    meta2=...
    meta3=...
    meta4=...
    meta5=... 
)}
html代码示例:

<td>
    <p itemscope itemtype="http://schema.org/JobPosting">
        <a href="/valstybes-tarnyba/vsi-taurages-rajono-pirmines-sveikatos-prieziuros-centro-direktorius-taurageje-2-338912727/?sri=83" target="_blank" itemprop="title" onclick="$(this).parents('tr.data').addClass('read');">VšĮ Tauragės rajono pirminės sveikatos priežiūros centro direktorius</a>
        <a href="/viesoji-istaiga-taurages-rajono-pirmines-sveikatos-prieziuros-centras-darbo-skelbimai" target="_blank" itemprop="hiringOrganization" itemscope itemtype="http://schema.org/Organization"><span itemprop="name">Viešoji įstaiga Tauragės rajono pirminės sveikatos priežiūros centras</span></a>
        <meta itemprop="jobLocation" content="Tauragė" />
        <meta itemprop="datePosted" content="2019-08-22" />
        <meta itemprop="employmentType" content="FULL_TIME" />
        <meta itemprop="validThrough" content="2019-09-06T00:00:00.000" />
        <meta itemprop="url" content="https://www.cv.lt/valstybes-tarnyba/vsi-taurages-rajono-pirmines-sveikatos-prieziuros-centro-direktorius-taurageje-2-338912727" />
    </p>
</td>
<td>
你可以运行这个

my_content <- html %>% html_nodes("tbody p meta")  %>%  html_attr("content") 
只为您提供Meta1。如果对每个元使用itemprop值,则可以获取其中的数据,如下所示:

 meta1 <-    html %>% html_nodes("[itemprop='jobLocation']") %>% html_attr("content") 
 meta2 <-    html %>% html_nodes("[itemprop='datePosted']") %>% html_attr("content") 
 meta3 <-    html %>% html_nodes("[itemprop='employmentType']") %>% html_attr("content") 
 meta4 <-    html %>% html_nodes("[itemprop='validThrough']") %>% html_attr("content") 
 meta5 <-    html %>% html_nodes("[itemprop='url']") %>% html_attr("content") 

谢谢,但它会创建一个列表,然后在一列中填写。不幸的是,我不能像这样选择meta:html\u nodestbody p meta:nth-child1%>%html\u attrcontent,否则它会工作得很好!有什么想法吗?@justasmundeik没问题。我编辑了你期望的答案。非常感谢!根据您的回答,我还找到了另一种方法:使用Xpath,因为它允许选择位置meta1=html\u nodeshtml、Xpath=//p//meta[1]>%html\u attractcontent等等。@justasmundeik是我不知道的事情……这也是实现目标的一个好方法。谢谢
html %>% html_nodes("[itemprop='jobLocation']") %>% html_attr("content") 
 meta1 <-    html %>% html_nodes("[itemprop='jobLocation']") %>% html_attr("content") 
 meta2 <-    html %>% html_nodes("[itemprop='datePosted']") %>% html_attr("content") 
 meta3 <-    html %>% html_nodes("[itemprop='employmentType']") %>% html_attr("content") 
 meta4 <-    html %>% html_nodes("[itemprop='validThrough']") %>% html_attr("content") 
 meta5 <-    html %>% html_nodes("[itemprop='url']") %>% html_attr("content")