ValueError: unknown URL type: 'url'

advertisements
import urllib.request
import urllib.parse
import re           #regular equatuions

url = 'http://pythonprogramming.net/'
values = {'s': 'basics',
      'submit':'search'}        #this is how you search on most websites
data = urllib.parse.urlencode(values)
data= data.encode('utf-8')
req = urllib.request.Request('url', 'data')
resp = urllib.request.urlopen(req)
respData = resp.read()

#print(respData)

paragraphs = re.findall(r'<p>(.*?)</p>', str(respData))
for eachP in paragraphs:
    print(eachP)

This code was from a video I followed along with. I dont understand why it does not work since its copied. I tried to understand the error but they did make any sense. The url works and it a real website. Here are the error messages I received:

C:\Python34\python.exe "C:/Users/S/PycharmProjects/untitled/Parsing practice.py"
Traceback (most recent call last):
  File "C:/Users/Sean/PycharmProjects/untitled/Parsing practice.py", line 10, in <module>
    req = urllib.request.Request('url', 'data')
  File "C:\Python34\lib\urllib\request.py", line 266, in __init__
    self.full_url = url
  File "C:\Python34\lib\urllib\request.py", line 292, in full_url
    self._parse()
  File "C:\Python34\lib\urllib\request.py", line 321, in _parse
    raise ValueError("unknown url type: %r" % self.full_url)
ValueError: unknown url type: 'url'

I don't really know what these mean since I don't have 200 lines of code, just 19.


You passed in the string 'url', not the variable:

req = urllib.request.Request('url', 'data')
#                            ^^^^^

That's not a recognized URL format. You meant to pass in the variable (no quotes):

req = urllib.request.Request(url, data)

Note that you did the same with data.

Because you passed in the wrong value on line 10 (first two lines of the traceback), you triggered a problem deeper in the urllib.request module (remaining lines of the traceback); that module does have 100s of lines.