我需要从下面的html代码中提取以下粗体数据:
<div class="name-ad hidden" data-count="91">
<div class="name-data-item" data-name="**I NEED TO SCRAPE THIS**" data-
count="92">
<div class="name-data-name">Washington NH</div>
<div class="name-data-location">Sullivan, Washington,
NH<br></div><div class="name-data-status">**I NEED TO
SCRAPE THIS AS WELL**</div> </div>
可以使用sed命令完成吗?如果没有,我该怎么做?
提前谢谢!
答案 0 :(得分:1)
使用awk
:
$ cat file
<div class="name-ad hidden" data-count="91">
<div class="name-data-item" data-name="**I NEED TO SCRAPE THIS**" data-
count="92">
<div class="name-data-name">Washington NH</div>
<div class="name-data-location">Sullivan, Washington,
NH<br></div><div class="name-data-status">**I NEED TO
SCRAPE THIS AS WELL**</div> </div>
$ awk -F\" '/name-data-item/ {print $4}' file
**I NEED TO SCRAPE THIS**
答案 1 :(得分:1)
使用xmlstarlet
和更多vaild html(file.html):
<html>
<body>
<div class="name-ad hidden" data-count="91">
<div class="name-data-item" data-name="**I NEED TO SCRAPE THIS**" data-count="92">
<div class="name-data-name">Washington NH</div>
<div class="name-data-location">Sullivan, Washington, NH<br /></div>
<div class="name-data-status">**I NEED TO SCRAPE THIS AS WELL**</div>
</div>
</div>
</body>
</html>
命令:
xmlstarlet sel --html -t \
-v "//html/body/div/div/@data-name" \
-v "//html/body/div/div/div[@class='name-data-status']" file.html
输出:
**I NEED TO SCRAPE THIS****I NEED TO SCRAPE THIS AS WELL**
或换行:
xmlstarlet sel --html -t \
-v "//html/body/div/div/@data-name" \
-n \
-v "//html/body/div/div/div[@class='name-data-status']" file.html
输出:
**I NEED TO SCRAPE THIS** **I NEED TO SCRAPE THIS AS WELL**