URLLIB.REQUEST Not Working - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: URLLIB.REQUEST Not Working (/thread-5088.html) |
URLLIB.REQUEST Not Working - hallofriends - Sep-18-2017 import urllib.request try: url = 'https://www.whoscored.com/Players/5583/Show/Cristiano-Ronaldo' # now, with the below headers, we defined ourselves as a simpleton who is # still using internet explorer. headers = {} headers['User-Agent'] = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.27 Safari/537.17" req = urllib.request.Request(url, headers = headers) resp = urllib.request.urlopen(req) respData = resp.read() print(respData) saveFile = open('withHeaders.txt','w') saveFile.write(str(respData)) saveFile.close() except Exception as e: print(str(e))The above code is not returning the source of the page properly. It is returning as
RE: URLLIB.REQUEST Not Working - Larz60+ - Sep-18-2017 Use requests, see https://python-forum.io/Thread-Web-Scraping-part-1 and/or https://python-forum.io/Thread-Web-scraping-part-2 |