使用 Python 查找文本中的超链接(与 Twitter 相关) [英] Find Hyperlinks in Text using Python (twitter related)

查看:55
本文介绍了使用 Python 查找文本中的超链接(与 Twitter 相关)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何解析文本并找到所有带有字符串的超链接实例?超链接不会是 <a href="http://test.com">test</a> 的 html 格式,而是 http://test.com

How can I parse text and find all instances of hyperlinks with a string? The hyperlink will not be in the html format of <a href="http://test.com">test</a> but just http://test.com

其次,我想然后转换原始字符串并将超链接的所有实例替换为可点击的html超链接.

Secondly, I would like to then convert the original string and replace all instances of hyperlinks into clickable html hyperlinks.

我在此线程中找到了一个示例:

I found an example in this thread:

最简单的方法来转换一个指向 C# 字符串中超链接的 URL?

但无法在python中重现它:(

but was unable to reproduce it in python :(

推荐答案

这是 将 URL 转换为 C# 字符串中超链接的最简单方法?:

import re

myString = "This is my tweet check it out http://tinyurl.com/blah"

r = re.compile(r"(http://[^ ]+)")
print r.sub(r'<a href="\1">\1</a>', myString)

输出:

This is my tweet check it out <a href="http://tinyurl.com/blah">http://tinyurl.com/blah</a>

这篇关于使用 Python 查找文本中的超链接(与 Twitter 相关)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆