【问题标题】:How to use Xpath in PostgreSQL to extract html Attributes?如何在 PostgreSQL 中使用 Xpath 提取 html 属性?
【发布时间】:2021-11-09 01:35:08
【问题描述】:

我正在处理仅以如下所示格式存储 HTML href 标记的列。 href标签之外没有其他标签。

<a href='https://www.website1.ca/en/Section1/Section2/Section3/Section-4---Section5-R-Zone.pdf'>B4</a>

我正在尝试从 HTML 标记中提取链接:https://www.website1.ca/en/Section1/Section2/Section3/Section-4---Section5-R-Zone.pdf

我不确定要使用什么 Xpath 表达式。我尝试使用以下方法获取 HTML 属性中的值,但它似乎不起作用。

select
xpath(
'/href',
$$<a href='https://www.website1.ca/en/Section1/Section2/Section3/Section-4---Section5-R-Zone.pdf'>B4</a>$$,
'/@href'
); 

是否有 xpath 表达式来获取 HTML 属性内的值?

【问题讨论】:

    标签: html xml postgresql parsing xpath


    【解决方案1】:

    您的 XPATH 查询错误。使用此查询获取所有a 标签的所有href 属性:

    SELECT xpath(
              '//a/@href',
               $$<a href='https://www.website1.ca/en/Section1/Section2/Section3/Section-4---Section5-R-Zone.pdf'>B4</a>$$
           );
    
                                              xpath                                          
    ═════════════════════════════════════════════════════════════════════════════════════════
     {https://www.website1.ca/en/Section1/Section2/Section3/Section-4---Section5-R-Zone.pdf}
    (1 row)
    

    结果是一个包含所有结果的数组(可能有几个这样的标签)。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-23
    • 1970-01-01
    • 2021-03-24
    • 1970-01-01
    • 1970-01-01
    • 2012-07-26
    • 2020-03-03
    相关资源
    最近更新 更多