用锚元素从锚元素中提取液体宏

发布于 2025-02-02 15:31:25 字数 1209 浏览 1 评论 0原文

我正在开发一项服务,该服务使人们可以使用液体模板引擎呈现HTML页面,并且我想从任何a elements提取任何href s,因此我们可以稍后在单击链接上运行统计信息。

我想出了这样的东西。这个想法是将URL编码的值添加到data-Original-Macro属性,如果它是href属性的液体宏,则

def add_original_macro_to_links(html: str) -> str:
    html_tree = bs4.BeautifulSoup(html, features="lxml")

    for a in html_tree.find_all("a"):
        href = a.get("href")
        print(href)
        if not href or not re.match(r"{{.+}}", href):
            continue

        a["data-original-macro"] = parse.quote(href)

    return str(html_tree).strip()

在大多数情况下都可以使用此作用,除非宏有一个字符,如此示例:

<ul>
{%- for person in people %}
  <li>
    <a href="{{person | prepend: "http://example.com/"}}">
      {{ person | capitalize }}
    </a>
  </li>
{%- endfor%}
</ul>

在这种情况下,BeautifulSoup将解析a元素。 &lt; a href =“ {{person | prepend:” http:=“”&gt; {{person |大写}}}&lt;/a&gt;,这无助于我的目的。

我的问题是,是否有一些方法可以始终如一地正确提取HREF?最糟糕的情况,我可能只是忽略了这种情况,因为我认为只有奇数客户会使用这种格式,因为它不在我们网站的默认宏集中。

I'm working on a service that lets people render their HTML pages using the Liquid templating engine, and I want to extract any hrefs from any a elements, so we can run stats on the clicked links later.

Using BeautifulSoup, I came up with something like this. The idea is to add the URL-encoded value to the data-original-macroattribute if it's a Liquid macro in the href attribute

def add_original_macro_to_links(html: str) -> str:
    html_tree = bs4.BeautifulSoup(html, features="lxml")

    for a in html_tree.find_all("a"):
        href = a.get("href")
        print(href)
        if not href or not re.match(r"{{.+}}", href):
            continue

        a["data-original-macro"] = parse.quote(href)

    return str(html_tree).strip()

This works for most cases, except when a macro has a " character in it, like this example:

<ul>
{%- for person in people %}
  <li>
    <a href="{{person | prepend: "http://example.com/"}}">
      {{ person | capitalize }}
    </a>
  </li>
{%- endfor%}
</ul>

In this case, BeautifulSoup will parse the a element as this:
<a href="{{person | prepend: " http:="">{{ person | capitalize }}</a>, which doesn't work for my purposes.

My question is, is there some way to correctly extract the href consistently? Worst case I may just ignore this case, since I'm thinking only the odd customer will use this format, as it's not in our website's default set of macros.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文