【发布时间】:2021-04-05 20:42:45
【问题描述】:
要从我使用正则表达式的 html 文档中获取某个 HTML 标记及其内容 -
html -
<div id="abc">content</div>
<a class="anchorclass">content</a>
<table id="table1">content</table>
<div id="div2">content</div>
<a class="anchorclass2">content</a>
<div class="divclass">content</div>
正则表达式
/<div id="abc"[\s\S]*?<\/div>/
它精确地返回这个特定的div 内容(id="abc" 的div)。
我想用一个正则表达式从上面捕获多个 html 元素,我该怎么做?有没有办法连接条件或者是否有任何“和”运算符来选择多个html标签?
我想编写一个选择 -
的单个正则表达式- id="abc"的div
- 一个 class="anchorclass"
- div 与 class="divclass"
从上面的html,那会是什么?
【问题讨论】:
-
Regex 是错误的工具。使用HTML parser 之类的 HTML Agility Pack。
标签: javascript html regex web-scraping