1

Here's a text file on the internet (http) http://hughchalmers.com/example.txt. How would I print that in Python 2?

People are saying this is a duplicate, I would like to say that it is not a duplicate, any other script I found gave Error 412.

Hugh Chalmers
  • 49
  • 1
  • 9

2 Answers2

3
>>> 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'
wim
  • 338,267
  • 99
  • 616
  • 750
0

You can use requests. Their first example shows how:

import requests
r = requests.get('http://hughchalmers.com/example.txt', headers={'user-agent': 'hugh'})
r.text