Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
urllib request error 404
#1
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:

Error:
Traceback (most recent call last): File "C:\Users\root\Desktop\RaspberryHomeProject2\test.py", line 31, in <module> response = urllib.request.urlopen(searchUrl).read() File "C:\Users\root\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 222, in urlopen return opener.open(url, data, timeout) File "C:\Users\root\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 531, in open response = meth(req, response) File "C:\Users\root\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 641, in http_response 'http', request, response, code, msg, hdrs) File "C:\Users\root\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 569, in error return self._call_chain(*args) File "C:\Users\root\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 503, in _call_chain result = func(*args) File "C:\Users\root\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 649, in http_error_default raise HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 404: Not Found
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!
Reply
#2
I solved the problem by not using urllib but the requests module to get the html data.
Reply
#3
Quote:but the requests module to get the html data.
Good move!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  urllib can't find "parse" rjdegraff42 6 1,973 Jul-24-2023, 05:28 PM
Last Post: deanhystad
  how can I correct the Bad Request error on my curl request tomtom 8 4,969 Oct-03-2021, 06:32 AM
Last Post: tomtom
  Prevent urllib.request from using my local proxy spacedog 0 2,805 Apr-24-2021, 08:55 PM
Last Post: spacedog
  urllib.request.ProxyHandler works with bad proxy spacedog 0 5,853 Apr-24-2021, 08:02 AM
Last Post: spacedog
  Need help with XPath using requests,time,urllib.request and BeautifulSoup spacedog 3 2,800 Apr-24-2021, 02:48 AM
Last Post: bowlofred
  Help with urllib.request Brian177 2 2,839 Apr-21-2021, 01:58 PM
Last Post: Brian177
  urllib.request ericmt123 2 2,389 Dec-21-2020, 06:53 PM
Last Post: Larz60+
  Cannot open url link using urllib.request Askic 5 6,572 Oct-25-2020, 04:56 PM
Last Post: Askic
  urllib is not a package traceback cc26 3 5,295 Aug-28-2020, 09:34 AM
Last Post: snippsat
  Empty response to request causing .json() to error t4keheart 1 9,953 Jun-26-2020, 08:35 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020