用锚元素从锚元素中提取液体宏
我正在开发一项服务,该服务使人们可以使用液体模板引擎呈现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 href
s 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-macro
attribute 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论