как получить текст с URL в Python 3? - PullRequest
0 голосов
/ 02 марта 2020

У меня есть программа, которая прекрасно работает с локальным текстовым файлом, и я хочу, чтобы он обрабатывал текстовый файл в сети таким же образом. Когда я запускаю следующее, я получаю «AttributeError: объект int не имеет атрибута translate». Как сделать так, чтобы версия URL работала так же, как локальный текстовый файл?

filename = input("What is the filename? ")
import string

from urllib.request import urlopen

tr = str.maketrans("", "", string.punctuation)

# check whether the file is a web address or a regular file and open accordingly
if filename[:4] == "http":
    response = urlopen(filename)
else:
    response = open(filename)

#create an empty dictionary called "counts"
counts = dict()

#process the text line by line
for line in response:
    # strip the punctuation using the function defined above
    line = line.translate(tr)
...