gpt4 book ai didi

python - 如何在 Python 中查找相对 URL 并将其转换为绝对 URL

转载 作者:行者123 更新时间:2023-11-28 19:38:30 25 4
gpt4 key购买 nike

我从网页 ( http://www.opensolaris.org/os/community/on/flag-days/all/ ) 中提取了一些代码,如下所示,

<tr class="build">
<th colspan="0">Build 110</th>
</tr>
<tr class="arccase project flagday">
<td>Feb-25</td>
<td></td>
<td></td>
<td></td>
<td>
<a href="../pages/2009022501/">Flag Day and Heads Up: Power Aware Dispatcher and Deep C-States</a><br />
cpupm keyword mode extensions - <a href="/os/community/arc/caselog/2008/777/">PSARC/2008/777</a><br />
CPU Deep Idle Keyword - <a href="/os/community/arc/caselog/2008/663/">PSARC/2008/663</a><br />
</td>
</tr>

里面有一些相对url路径,现在我想用正则表达式搜索它并用绝对url路径替换它们。因为我知道 urljoin 可以像那样做替换工作,

>>> urljoin("http://www.opensolaris.org/os/community/on/flag-days/all/",
... "/os/community/arc/caselog/2008/777/")
'http://www.opensolaris.org/os/community/arc/caselog/2008/777/'

现在我想知道如何使用正则表达式搜索它们,最后将代码转换为,

<tr class="build">
<th colspan="0">Build 110</th>
</tr>
<tr class="arccase project flagday">
<td>Feb-25</td>
<td></td>
<td></td>
<td></td>
<td>
<a href="http://www.opensolaris.org/os/community/on/flag-days/all//pages/2009022501/">Flag Day and Heads Up: Power Aware Dispatcher and Deep C-States</a><br />
cpupm keyword mode extensions - <a href="http://www.opensolaris.org/os/community/arc/caselog/2008/777/">PSARC/2008/777</a><br />
CPU Deep Idle Keyword - <a href="http://www.opensolaris.org/os/community/arc/caselog/2008/663/">PSARC/2008/663</a><br />
</td>
</tr>

我对正则表达式的了解太少了,所以我想知道如何做到这一点。谢谢

我用Beautiful Soup完成了工作,哈哈~谢谢大家!

最佳答案

我不确定您要实现什么但使用 BASE tag in HTML可以为您完成此技巧,而无需在处理时求助于正则表达式。

关于python - 如何在 Python 中查找相对 URL 并将其转换为绝对 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/589833/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com