问题是:检查输入的链接是否有效,或者该链接可以作为https://stackoverflow.com/和 stackoverflow.com 输入。
我试图解决它
input_url = str(input("Enter url: ")
result = re.findall(r'(http[s]?://)?\S+', input_url)
返回错误 -Invalid URL '': No schema supplied. Perhaps you meant http://?
没有urllib
或别的什么,它必须只是正则表达式
完整代码:
import re, requests
from collections import Counter
from prettytable import PrettyTable
url_input = str(input("Enter url: "))
url_checked = re.findall(r'(http[s]?://)?\S+', url_input)[0] # берем первый элемент
response = requests.get(str(url_checked)) # запрос на введенную ссылку
result = re.findall( r"\"(?:http[s]?://)?([^:/\s\"]+)/?[^\"]*\"", response.text) # фильтрация ссылок
result.sort() # sorting by alphabet
# link - https://stackoverflow.com/
pt = PrettyTable(field_names = ["word", "counter"])
pt.add_rows(list(Counter(result).most_common()))
print(pt)