有没有像“CSS选择器”那样的东西?或XPath grep? [英] Is there something like a "CSS selector" or XPath grep?
问题描述
div我需要在一堆HTML文件中找到所有地方,它们位于以下结构(CSS)中: .a ul.b
或XPath:
// div [@ class =a] // div [@ class =b]
grep
在这里没有帮助。是否有一个命令行工具可以返回符合此标准的所有文件(以及可选的所有文件)?即,如果文件匹配某个特定的HTML或XML结构,则返回文件名。试试这个:
- 安装 http://www.w3.org/Tools/HTML-XML-utils/ 。
- 保存网页(称为filename.html)。
- 运行:
hxnormalize -l 240 -x filename.html | hxselect -s'\\\
'-clabel.black
其中label.black
是唯一标识HTML元素名称的CSS选择器。写一个名为 cssgrep
的帮手脚本:
#!/ bin / bash
#忽略错误,将结果写入标准输出。
hxnormalize -l 240 -x $ 1 2> / dev / null | hxselect -s'\ n'-c$ 2
您可以运行:
cssgrep filename.htmllabel.black
这将为类 black
的所有HTML 标签
元素生成内容。另请参阅: https://superuser.com/a/529024/9067
I need to find all places in a bunch of HTML files, that lie in following structure (CSS):
div.a ul.b
or XPath:
//div[@class="a"]//div[@class="b"]
grep
doesn't help me here. Is there a command-line tool that returns all files (and optionally all places therein), that match this criterium? I.e., that returns file names, if the file matches a certain HTML or XML structure.
Try this:
- Install http://www.w3.org/Tools/HTML-XML-utils/.
- Save a web page (call it filename.html).
- Run:
hxnormalize -l 240 -x filename.html | hxselect -s '\n' -c "label.black"
Where "label.black"
is the CSS selector that uniquely identifies the name of the HTML element. Write a helper script named cssgrep
:
#!/bin/bash
# Ignore errors, write the results to standard output.
hxnormalize -l 240 -x $1 2>/dev/null | hxselect -s '\n' -c "$2"
You can then run:
cssgrep filename.html "label.black"
This will generate the content for all HTML label
elements of the class black
. See also: https://superuser.com/a/529024/9067
这篇关于有没有像“CSS选择器”那样的东西?或XPath grep?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!