0

I am trying the below code to access the URL http://learncodethehardway.org/words.txt in Python 3.5 but I am seeing a 403 HTTP error. How should I configure my headers to avoid this?

import urllib.request
url = "http://learncodethehardway.org/words.txt"

hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
  'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
  'Accept-Encoding': 'none',
  'Accept-Language': 'en-US,en;q=0.8',
  'Connection': 'keep-alive'}
req = urllib.request.Request(url, None, headers=hdr)
html = urllib.request.urlopen(req)

print (html.read())
moopet
  • 6,014
  • 1
  • 29
  • 36

1 Answers1

0

This works for me:

import urllib.request
url = 'http://learncodethehardway.org/words.txt'
req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
html = urllib.request.urlopen(req).read()
print(html.decode())

for more information refer to the similar question: HTTP error 403 in Python 3 Web Scraping

GeoLinux
  • 21
  • 4