您好,我正在嘗試獲取我班上的內容。我需要的只是鏈接(沒有任何標簽)和 bash 中 span 類標題的值。我做了這樣的事情(用于測驗),但這并沒有給出任何答案。我做錯了什么?
curl -s https://www.website.com/q?search=violet | grep -e "^<span class=\"top\">(.*?)</span>"
<div class="video-item-list">
<span class="age0" title="0"></span>
<span class="hsa" title="tex"></span>
<span class="Encour" title="test"></span>
<a href="https://www.website.com/a/1973">
<img class="image lazy" width="100" height="40"
data-original="https://img.com/i?jpg=123">
</a>
<span class="top">
<a href="https://www.website.com/a/1973">
<span class="title">Violet test</span>
</a>
<span class="episode"> 250
</span>
<a class="team"></a>
</span>
<span class="info"> 2017</span>
</div>
<div id="n" class="video-item-list-days">
<h5>Letter n</h5>
</div>
uj5u.com熱心網友回復:
正如評論中提到的,正則運算式是處理 HTML 的錯誤工具。一種使用 XSLT 樣式表的方法和xsltproc:
例子.xslt:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:template match="/">
<xsl:for-each select="//span[@class='top']">
<xsl:value-of select="a[@href]/@href" />
<xsl:text>	</xsl:text>
<xsl:value-of select="a[@href]/span[@class='title']" />
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
用法:
$ curl -s https://www.website.com/q?search=violet | xsltproc --html example.xslt -
https://www.website.com/a/1973 Violet test
uj5u.com熱心網友回復:
建議 RegExp 模式僅匹配 FIRSTspan類。
grep -oP '(?<=<span ] '
為您的樣品測驗:
age0
hsa
Encour
top
title
episode
info
不確定這是否是你的意圖。
如果您只需要同一行中的 FIRSTspan類封閉元素。
grep -oP '(?<=<span ] (?=".*</span>)' input.1.txt
為您的樣品測驗:
age0
hsa
Encour
title
info
uj5u.com熱心網友回復:
謝謝大家,我這樣做并且它有效。可能是個壞主意,但我稍后會看到
(?<=<span class="top">).*?(?=<\/span>)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/450367.html
上一篇:如何為目錄中的每個檔案添加后綴
下一篇:在getopt中處理glob
