在 python 上通过互联网阅读文本文件
Read text file over internet on python
这是 Internet (http) 上的一个文本文件 http://hughchalmers.com/example.txt。我如何在 Python 2 中打印它?
人们说这是重复的,我想说它不是重复的,我发现的任何其他脚本都给出了错误 412。
>>> from urllib2 import Request, urlopen
>>> url = 'http://hughchalmers.com/example.txt'
>>> urlopen(Request(url=url, headers={'User-Agent': "hey, it's wim"})).read()
'Exmaple Text'
您可以使用 requests。他们的第一个例子展示了如何:
import requests
r = requests.get('http://hughchalmers.com/example.txt', headers={'user-agent': 'hugh'})
r.text
这是 Internet (http) 上的一个文本文件 http://hughchalmers.com/example.txt。我如何在 Python 2 中打印它?
人们说这是重复的,我想说它不是重复的,我发现的任何其他脚本都给出了错误 412。
>>> from urllib2 import Request, urlopen
>>> url = 'http://hughchalmers.com/example.txt'
>>> urlopen(Request(url=url, headers={'User-Agent': "hey, it's wim"})).read()
'Exmaple Text'
您可以使用 requests。他们的第一个例子展示了如何:
import requests
r = requests.get('http://hughchalmers.com/example.txt', headers={'user-agent': 'hugh'})
r.text