我的产品网站中有 html 页面,我想解析文档并从 html 页面获取产品版本。
html 页面看起来像这样:
<html>
.......
.......
<body>
.......
.......
<div id='version_info'>
<div class="product-version">
<div class="product-title">Name of the product 1:</div><div class="product-value">ver_123</div>
</div>
<div class="product-version">
<div class="product-title">Name of the product 2:</div><div class="product-value">ver_456</div>
</div>
<div class="product-version">
<div class="product-title">Name of the product 3:</div><div class="product-value">ver_845</div>
</div>
<div class="product-version">
<div class="product-title">Name of the product 4:</div><div class="product-value">ver_146</div>
</div>
</div>
.......
.......
</body>
.......
.......
</html>
如何 grep 文档并形成类似这样的字符串? 产品名称1=ver_123,产品名称2=ver_456,产品名称3=ver_845等
最佳答案
我已经处理了这个特定的 HTML 文件,结果我在变量 result
下得到了所需变量的字典
注意:
1. Please change path of html file in playbook.
2. This particular playbook work for this HTML example. For further requirements and improvements provide HTML.
---
- hosts: localhost
name: "Getting varibles from HTML"
vars:
result: {}
tasks:
- name: "Getting content of the file"
command: cat /path/to/html/file
register: search
- name: "Creating dictionary while Looping over file"
ignore_errors: true
vars:
key: "{{item | replace('<div class=\"product-title\">','') | replace('</div>','') | regex_replace('<div.*','') | regex_replace('^\\s*','')}}"
value: "{{item | replace('<div class=\"product-title\">','') | replace('</div>','') | regex_replace('^[\\w\\s\\:]*','') | replace('<div class=\"product-value\">','') | regex_replace('\\s*$','')}}"
set_fact:
result: "{{ result | combine( { key: value } ) }}"
when: "'product-title' in item"
with_items: "{{search.stdout_lines}}"
- name: "Getting register"
debug:
msg: "{{result}}"
...
输出
ok: [localhost] => {
"msg": {
"Name of the product 1:": "ver_123",
"Name of the product 2:": "ver_456",
"Name of the product 3:": "ver_845",
"Name of the product 4:": "ver_146"
}
}
关于ansible - 在ansible中解析和grep html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42239997/