我在Python中使用lxml来解析一些HTML,我想提取所有链接到图像.我现在这样做的方式是:
//a[contains(@href,'.jpg') or contains(@href,'.jpeg') or ... (etc)]
Run Code Online (Sandbox Code Playgroud)
这种方法存在一些问题:
我想使用正则表达式,但我失败了:
//a[regx:match(@href,'.*\.(?:png|jpg|jpeg)')]
Run Code Online (Sandbox Code Playgroud)
这一直给我回复了所有链接......
有谁知道正确,优雅的方式来做这个或我的正则表达式方法有什么问题?
代替:
a[contains(@href,'.jpg')]
Run Code Online (Sandbox Code Playgroud)
使用:
a[substring(@href, string-length(@href)-3)='.jpg']
Run Code Online (Sandbox Code Playgroud)
(对于其他可能的结局也有相同的表达模式)。
上述表达式是 XPath 1.0 等效于以下 XPath 2.0 表达式:
a[ends-with(@href, '.jpg')]
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5137 次 |
最近记录: |