Mal*_*e94 4 html xpath dom css-selectors
嗨,我尝试获取包含href = / p / {random} /?tagged = see的所有元素,请参阅此处。
//div[preceding::h2[text()='Most recent']]/div/div/a[@href='/p/*/?tagged=see']
Run Code Online (Sandbox Code Playgroud)
我该如何解决此代码,必须将“ *”替换为其他内容?
在XPath 2.0或更高版本中,可以使用Regex函数,例如:
//a[matches(@href, '/p/.*/\?tagged=see')]
Run Code Online (Sandbox Code Playgroud)
或结合使用字符串函数starts-with()
和ends-with()
:
//a[starts-with(@href, '/p/')]
[ends-with(@href, '/?tagged=see')]
Run Code Online (Sandbox Code Playgroud)
XPath 1.0没有正则表达式也没有ends-with()
函数,但是,您可以模拟后者:
//a[starts-with(@href, '/p/')]
[substring(@href, string-length(@href) - string-length('/?tagged=see') +1) = '/?tagged=see']
Run Code Online (Sandbox Code Playgroud)
简化:
//a[starts-with(@href, '/p/')]
[substring(@href, string-length(@href) - 11) = '/?tagged=see']
Run Code Online (Sandbox Code Playgroud)