urllib request error 404 - 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 error 404 (/thread-18253.html) |
urllib request error 404 - Coco - May-10-2019 Hey there, I'm not able to obtain my response from one of two YouTube links in a document with the google API. Here is the code: import urllib.request from random import randint ''' content of youtubeMusicPlaylist.txt: https://www.youtube.com/watch?v=V6g172aoZb4 https://www.youtube.com/watch?v=ksrnQ7i82T0 ''' def getLineByIndex(lineIndex): try: with open('youtubeMusicPlaylist.txt', 'r') as file: for i, line in enumerate(file): if i == lineIndex: return line if i > lineIndex: print('out of bounds error while reading lines') exit(0) return -1 except IOError: print('cannot access file: youtubeMusicPlaylist.txt') exit(0) link = getLineByIndex(randint(0, 1)) index = link.find('v=') link = link[index+2:] index = link.find('&t=') if index != -1: link = link[:index] key="1234567" searchUrl="https://www.googleapis.com/youtube/v3/videos?id="+link+"&key="+key+"&part=contentDetails" response = urllib.request.urlopen(searchUrl).read() print(response)Some links are working fine, but most of them do not. Here is the error message: When I set the video link id manually into the string, it will work without problems by the way.The problem may be located in the link variable that will be passed to the request. Can somebody help me out? Thanks! RE: urllib request error 404 - Coco - May-11-2019 I solved the problem by not using urllib but the requests module to get the html data. RE: urllib request error 404 - Larz60+ - May-11-2019 Quote:but the requests module to get the html data.Good move! |