Hugh Chalmers
Hugh Chalmers

Reputation: 49

Read text file over internet on python

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.

Upvotes: 1

Views: 596

Answers (2)

Hank Anderson
Hank Anderson

Reputation: 1

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

Upvotes: 0

wim
wim

Reputation: 362857

>>> 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'

Upvotes: 3

Related Questions