查找字符串中所有 HTML 和非 HTML 编码的 URL
Find all HTML and non-HTML encoded URLs in string
我想找到字符串中的所有 URL。我在 Whosebug 上找到了各种解决方案,这些解决方案因字符串的内容而异。
例如,假设我的字符串包含 HTML,this answer 建议使用 BeautifulSoup
或 lxml
。
另一方面,如果我的字符串只包含没有 HTML 标签的普通 URL,this answer 建议使用正则表达式。
鉴于我的字符串同时包含 HTML 编码的 URL 和普通的 URL,我找不到好的解决方案。这是一些示例代码:
import lxml.html
example_data = """<a href="http://www.some-random-domain.com/abc123/def.html">Click Me!</a>
http://www.another-random-domain.com/xyz.html"""
dom = lxml.html.fromstring(example_data)
for link in dom.xpath('//a/@href'):
print "Found Link: ", link
正如预期的那样,结果是:
Found Link: http://www.some-random-domain.com/abc123/def.html
我也尝试了@Yannisp 提到的 twitter-text-python
库,但它似乎并没有同时提取 URLS:
>>> from ttp.ttp import Parser
>>> p = Parser()
>>> r = p.parse(example_data)
>>> r.urls
['http://www.another-random-domain.com/xyz.html']
从包含 HTML 和非 HTML 编码数据的字符串中提取两种 URL 的最佳方法是什么?是否有一个好的模块已经做到了这一点?还是我被迫将正则表达式与 BeautifulSoup
/lxml
?
结合使用
我投票是因为它激发了我的好奇心。似乎有一个名为 twitter-text-python 的库,它解析 Twitter 帖子以检测 url 和 href。否则,我会选择组合 regex + lxml
您可以使用 RE 查找所有网址:
import re
urls = re.findall("(https?://[\w\/$\-\_\.\+\!\*\'\(\)]+)", example_data)
它包括字母数字、'/' 和 "Characters allowed in a URL"
根据@YannisP 的回答,我得出了这个解决方案:
import lxml.html
from ttp.ttp import Parser
def extract_urls(data):
urls = set()
# First extract HTML-encoded URLs
dom = lxml.html.fromstring(data)
for link in dom.xpath('//a/@href'):
urls.add(link)
# Next, extract URLs from plain text
parser = Parser()
results = parser.parse(data)
for url in results.urls:
urls.add(url)
return list(urls)
这导致:
>>> example_data
'<a href="http://www.some-random-domain.com/abc123/def.html">Click Me!</a>\nhttp://www.another-random-domain.com/xyz.html'
>>> urls = extract_urls(example_data)
>>> print urls
['http://www.another-random-domain.com/xyz.html', 'http://www.some-random-domain.com/abc123/def.html']
我不确定它在其他 URL 上的效果如何,但它似乎可以满足我的需要。
我想找到字符串中的所有 URL。我在 Whosebug 上找到了各种解决方案,这些解决方案因字符串的内容而异。
例如,假设我的字符串包含 HTML,this answer 建议使用 BeautifulSoup
或 lxml
。
另一方面,如果我的字符串只包含没有 HTML 标签的普通 URL,this answer 建议使用正则表达式。
鉴于我的字符串同时包含 HTML 编码的 URL 和普通的 URL,我找不到好的解决方案。这是一些示例代码:
import lxml.html
example_data = """<a href="http://www.some-random-domain.com/abc123/def.html">Click Me!</a>
http://www.another-random-domain.com/xyz.html"""
dom = lxml.html.fromstring(example_data)
for link in dom.xpath('//a/@href'):
print "Found Link: ", link
正如预期的那样,结果是:
Found Link: http://www.some-random-domain.com/abc123/def.html
我也尝试了@Yannisp 提到的 twitter-text-python
库,但它似乎并没有同时提取 URLS:
>>> from ttp.ttp import Parser
>>> p = Parser()
>>> r = p.parse(example_data)
>>> r.urls
['http://www.another-random-domain.com/xyz.html']
从包含 HTML 和非 HTML 编码数据的字符串中提取两种 URL 的最佳方法是什么?是否有一个好的模块已经做到了这一点?还是我被迫将正则表达式与 BeautifulSoup
/lxml
?
我投票是因为它激发了我的好奇心。似乎有一个名为 twitter-text-python 的库,它解析 Twitter 帖子以检测 url 和 href。否则,我会选择组合 regex + lxml
您可以使用 RE 查找所有网址:
import re
urls = re.findall("(https?://[\w\/$\-\_\.\+\!\*\'\(\)]+)", example_data)
它包括字母数字、'/' 和 "Characters allowed in a URL"
根据@YannisP 的回答,我得出了这个解决方案:
import lxml.html
from ttp.ttp import Parser
def extract_urls(data):
urls = set()
# First extract HTML-encoded URLs
dom = lxml.html.fromstring(data)
for link in dom.xpath('//a/@href'):
urls.add(link)
# Next, extract URLs from plain text
parser = Parser()
results = parser.parse(data)
for url in results.urls:
urls.add(url)
return list(urls)
这导致:
>>> example_data
'<a href="http://www.some-random-domain.com/abc123/def.html">Click Me!</a>\nhttp://www.another-random-domain.com/xyz.html'
>>> urls = extract_urls(example_data)
>>> print urls
['http://www.another-random-domain.com/xyz.html', 'http://www.some-random-domain.com/abc123/def.html']
我不确定它在其他 URL 上的效果如何,但它似乎可以满足我的需要。