【发布时间】:2018-02-08 12:59:44
【问题描述】:
我正在尝试找出以下 HTML 代码 sn-p 的注释中所写的内容,这只是该代码的一部分:
<table id="datalist1" cellspacing="0" border="0" style="border-width:1px;border-style:solid;width:100%;border-collapse:collapse;">
<tr>
<td style="font-size:7pt;">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr align="left">
<td width="50%" class="subhead1">
<!-- <b>IE CODE : 0514026049</b> --> ' I want text inside this comment
</td>
<td rowspan="9" valign="top">
<span id="datalist1_ctl00_lbl_p"></span>
</td>
</tr>
我正在尝试以下方法
1) 获取元素的 Xpath。
2) 阅读网页
3) 转到评论节点
4) 提取评论中的文字
library(rvest)
library(xml2)
url <- 'http://agriexchange.apeda.gov.in/ExportersDirectory/exporters_list.aspx?letter=Z'
webpage <- read_html(url)
' Xpath of comment element I want to grab
//*[@id="datalist1"]/tbody/tr[1]/td/table/tbody/tr[1]/td[1]/comment()
webpage %>%
html_nodes(xpath='//*[@id="datalist1"]/tbody/tr[1]/td/table/tbody/tr[1]/td[1]/comment()')%>%html_text()
#character(0) ' this is output
但是上面的代码给出了一个空字符串。由于我从未使用过 Xpath,我不知道这是否是正确的方法。
我必须为所有评论元素运行这个。 我想简而言之我的问题是如何在 HTML 代码中提取 cmets?
【问题讨论】:
-
尝试从 XPath 中删除
tbody(/table/tbody/tr[1]-->/table//tr[1]),因为它可以通过浏览器添加到 DOM 中 -
...现在您正在寻找 XPath 解决方案,您可能需要再次检查 my answer 到您的 previous question :)
-
是的!当我检查网站的源代码时,
tbody不存在。我会尝试在没有 tbody 的情况下使用它 -
您只是想要 HTML 文档中的所有 cmets,还是对于您想要哪些特定规则?从你的例子很难看出。
-
我想要所有带有 标签的 cmets