用于在命令行上解析html的css选择器
que的Python项目详细描述
使用css选择器在命令行上对html进行切片和骰子。
快速启动
假设你想抓住所有的链接 http://example.com/foo/bar:
$ curl http://example.com/foo/bar | que "a->href"
假设给了你3行这样的代码:
/some/url?val=1 /some/url2?val=2 /some/url3?val=3
呃,那不是很有帮助,所以让我们稍微修改一下我们的论点:
$ curl http://example.com/foo/bar | que "a->http://example.com{href}"
现在,将打印:
http://example.com/some/url?val=1 http://example.com/some/url2?val=2 http://example.com/some/url3?val=3
选择
不知道如何使用css选择器?
选择器分为两部分,第一部分由->分隔 部分是在上述链接和 第二部分是要为每个 匹配:
$ css.selector->attribute,selector
选择器部分使用Python’s string formatting syntaxso 可以将所需的属性嵌入到较大的字符串中。
示例
查找页面上的所有“下载”链接:
que支持非标准的:contains css selector
$ curl http://example.com | que "a:contains(Download)->href"
选择以“foo”开头的属性data的所有链接:
$ curl http://example.com | que "a[data|=foo]->href"
安装
您可以使用pip安装stable:
$ pip install que
或者最新最伟大的 pypi:
$ pip install git+https://github.com/jaymon/que#egg=que
注释
- 如果需要一种功能更全面的html命令行解析器,请尝试 hq。