Bottom Page

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

def getLineByIndex(lineIndex):
        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')

            return -1
    except IOError:
        print('cannot access file: youtubeMusicPlaylist.txt')

link = getLineByIndex(randint(0, 1))

index = link.find('v=')
link = link[index+2:]
index = link.find('&t=')

if index != -1:
    link = link[:index]

response = urllib.request.urlopen(searchUrl).read()

Some links are working fine, but most of them do not.

Here is the error message:

Traceback (most recent call last): File "C:\Users\root\Desktop\RaspberryHomeProject2\", line 31, in <module> response = urllib.request.urlopen(searchUrl).read() File "C:\Users\root\AppData\Local\Programs\Python\Python37-32\lib\urllib\", line 222, in urlopen return, data, timeout) File "C:\Users\root\AppData\Local\Programs\Python\Python37-32\lib\urllib\", line 531, in open response = meth(req, response) File "C:\Users\root\AppData\Local\Programs\Python\Python37-32\lib\urllib\", line 641, in http_response 'http', request, response, code, msg, hdrs) File "C:\Users\root\AppData\Local\Programs\Python\Python37-32\lib\urllib\", line 569, in error return self._call_chain(*args) File "C:\Users\root\AppData\Local\Programs\Python\Python37-32\lib\urllib\", line 503, in _call_chain result = func(*args) File "C:\Users\root\AppData\Local\Programs\Python\Python37-32\lib\urllib\", 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?

I solved the problem by not using urllib but the requests module to get the html data.
Quote:but the requests module to get the html data.
Good move!

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  get file by proxy and header using urllib.request.urlretrieve randyjack 0 166 Mar-12-2020, 09:22 AM
Last Post: randyjack
  Error in request, cache key a21250450 2 825 Apr-02-2019, 11:20 AM
Last Post: a21250450
  Bad request(400) error! manhsv 8 693 Jan-15-2019, 01:41 PM
Last Post: hbknjr
  KeyError urllib DavidFernandez 4 975 Nov-21-2018, 08:34 PM
Last Post: DavidFernandez
  Download entire web pages and save them as html file with urllib.request fyec 2 6,127 Jul-13-2018, 10:12 AM
Last Post: Larz60+
  URLLIB.REQUEST Not Working hallofriends 1 3,175 Sep-18-2017, 05:00 PM
Last Post: Larz60+
  how to loop data in urllib? pythonlover 4 3,621 Jan-18-2017, 06:53 PM
Last Post: pythonlover

Forum Jump:

Users browsing this thread: 1 Guest(s)