Создание Pretty Table с переменными
Задача вывести ссылки и количество упоминаний с помощью Pretty Table
Эти ссылки результат выполнения программы:

Нужно что бы все это выводилось с помощью Pretty Table с заголовками word и match numbers
import re, requests
from collections import Counter
from prettytable import PrettyTable
url_input = input("Enter url: ")
url_checked = re.findall(r'https?://\S+', url_input)[0] # берем первый элемент
if url_input != url_checked: # проверка валидности ссылки
print("Entered url is invalid")
else:
pass
response = requests.get(str(url_checked)) # запрос на введенную ссылку
result = re.findall(r'/[A-Za-z.]+/', response.text) # фильтрация ссылок
result.sort() # sorting by alphabet
def count_words(List):
for word, counter in Counter(List).most_common(): # sorting and printing list by counter value
print(f"{word} matches {counter} times")
count_words(result)
pt = PrettyTable()
pt.header_names = ["word", "counter"]
def print_table(items):
print_table(result)
Ответы (1 шт):
Автор решения: MaxU
→ Ссылка
pt = PrettyTable(field_names=["word", "counter"])
pt.add_rows(list(Counter(result).most_common()))
print(pt)
вывод:
+------------------------------+---------+
| word | counter |
+------------------------------+---------+
| /cdn.sstatic.net/ | 43 |
| /stackoverflow.com/ | 33 |
| /home/ | 29 |
| /jobs/ | 5 |
| /stackexchange.com/ | 5 |
| /stackoverflow/ | 5 |
| /product/ | 4 |
| /endorsements/ | 2 |
| /products/ | 2 |
| /tagged/ | 2 |
| /teams/ | 2 |
| /users/ | 2 |
| /Channels/ | 1 |
| /Product/ | 1 |
| /ajax.googleapis.com/ | 1 |
| /chat.stackoverflow.com/ | 1 |
| /directory/ | 1 |
| /enterprise/ | 1 |
| /insights.stackoverflow.com/ | 1 |
| /libs/ | 1 |
| /linkedin.com/ | 1 |
| /pixel.quantserve.com/ | 1 |
| /sb.scorecardresearch.com/ | 1 |
| /secure.quantserve.com/ | 1 |
| /story/ | 1 |
| /twitter.com/ | 1 |
| /www.facebook.com/ | 1 |
| /www.instagram.com/ | 1 |
+------------------------------+---------+