《如何像计算机科学家一样思考:用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/