有没有像“CSS选择器”那样的东西?或XPath grep? [英] Is there something like a "CSS selector" or XPath grep?

查看:208
本文介绍了有没有像“CSS选择器”那样的东西?或XPath grep?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  div我需要在一堆HTML文件中找到所有地方,它们位于以下结构(CSS)中:

.a ul.b

或XPath:

  // div [@ class =a] // div [@ class =b] 

grep 在这里没有帮助。是否有一个命令行工具可以返回符合此标准的所有文件(以及可选的所有文件)?即,如果文件匹配某个特定的HTML或XML结构,则返回文件名。试试这个:

解决方案


  1. 安装 http://www.w3.org/Tools/HTML-XML-utils/

  2. 保存网页(称为filename.html)。

  3. 运行: 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:

  1. Install http://www.w3.org/Tools/HTML-XML-utils/.
  2. Save a web page (call it filename.html).
  3. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆