python - "Think Python"提取 URL 的练习

标签 python python-3.x

《如何像计算机科学家一样思考:用Python 3学习》第三版的4.21节中有一个练习

Suppose any line of text can contain at most one url that starts with “http://” and ends at the next space in the line. Write a fragment of code to extract and print the full url if it is present. (Hint: read the documentation for find. It takes some extra arguments, so you can set a starting point from which it will search.)

这是我编写的代码并且它有效..但我认为它并没有真正使用作者要求使用的“额外参数”。查看 here 中的查找文档我无法理解如何使用它。有人可以帮我编写此练习的正确代码吗?

def findurl(url):
    op_findex = url.find("//")
    op_lindex = url.rfind(" ")
    return url[op_findex+2:op_lindex]

print(findurl("http://example.com/site "))

最佳答案

错误在于您找到了字符串中的最后一个空格,而不是 URL 开头之后的第一个空格。

test = "Hello, visit http://google.com or http://stackoverflow.com for great fun"
start = test.find("http://")
end = test.find(" ", start)
print(test[start:end])

您还会注意到 http: 是提取的 URL 中必要且有用的部分。

关于python - "Think Python"提取 URL 的练习,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54670261/

相关文章:

python - OpenCV 中的数据类型

python - 使用 Pandas 内置除法时出现内存错误,但循环有效?

python - 负数和正数之间的按位 AND (&)?

python - 列表理解测试中的变量被视为未在 python3 的 exec 中定义

python - 为什么有些功能pass了

python - 自定义Python Twisted协议(protocol): good practices and complexity?

python - pandas重新定义isnull以忽略 'NA'

python - Python 的 `tarfile` 模块是否将它正在构建的文件存储在内存中?

python-3.x - 如何用 Pandas 计算每一行的标准偏差?

mysql - 接口(interface)错误: 2013: Lost connection to MySQL server during query