使用 Python 查找文本中的超链接(与 Twitter 相关)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/720113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 20:43:41  来源:igfitidea点击:

Find Hyperlinks in Text using Python (twitter related)

pythonregex

提问by TimLeung

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 格式,<a href="http://test.com">test</a>而只是http://test.com

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

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

I found an example in this thread:

我在这个线程中找到了一个例子:

Easiest way to convert a URL to a hyperlink in a C# string?

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

but was unable to reproduce it in python :(

但无法在 python 中重现它:(

回答by maxyfc

Here's a Python port of Easiest way to convert a URL to a hyperlink in a C# string?:

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

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=""></a>', myString)

Output:

输出:

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

回答by dfrankow

Hereis a much more sophisticated regexp from 2002.

是 2002 年的一个更复杂的正则表达式。

回答by Kekoa

Django also has a solution that doesn't just use regex. It is django.utils.html.urlize(). I found this to be very helpful, especially if you happen to be using django.

Django 也有一个不仅仅使用正则表达式的解决方案。它是django.utils.html.urlize()。我发现这非常有帮助,尤其是当您碰巧使用 django 时。

You can also extract the codeto use in your own project.

您还可以提取代码以在您自己的项目中使用。

回答by jmoz

Jinja2 (Flask uses this) has a filter urlizewhich does the same.

Jinja2(Flask 使用这个)有一个过滤器urlize,它做同样的事情。

Docs

文档