简体   繁体   English

ValueError:未知的url类型:'url'

[英]ValueError: unknown url type: 'url'

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. 我真的不知道这些是什么意思,因为我没有200行代码,只有19行。

You passed in the string 'url' , not the variable: 您传入了字符串 'url' ,而不是变量:

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

That's not a recognized URL format. 这不是公认的URL格式。 You meant to pass in the variable (no quotes): 您打算传递变量(不带引号):

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

Note that you did the same with data . 请注意,您对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); 因为您在第10行(回溯的前两行)传递了错误的值,所以您在urllib.request模块(回溯的其余行)中更深地触发了一个问题; that module does have 100s of lines. 该模块确实有100条线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM