如何转换HTML链接?

时间:2013-07-21 09:21:45

标签: python html hyperlink

假设我将许多html页面下载到目录

  

/路径/到/我/ DIR

.html页面包含如下链接:

"<a href="http://www.example.com/lessons/firstlesson.htm">first lesson</a>"

"<a href="http://www.example.com/lessons/secondlesson.htm">second lesson</a>"

感谢TobSpr,答案将其转换为:

"<a href="/path/to/my/dir/firstlesson.htm">first lesson</a>"

"<a href="/path/to/my/dir/secondlesson.htm">second lesson</a>"

更新

但是如何处理这种格式的链接:     “第一课”

"<a href="../index.htm">index</a>"

使用通常的字符串函数是非常困难,肮脏和错误的,所以我希望得到另一种更好的方式。

1 个答案:

答案 0 :(得分:1)

content.replace("http://www.example.com/", "file///path/to/dir/")

它也取代了包含的路径。

相关问题