2017-05-04 74 views
-2

這是我的代碼,我想從用戶輸入的URL中提取web的'標題',但它不起作用。python3中的「未知的URL類型:urlopen錯誤」錯誤

import re 
    import urllib.request 

    url = input('Please enter website URL : ') 
    h = urllib.request.urlopen(url) 
    code = h.read() 
    pattern = re.compile(r'<title>(.+)</title>', re.M) 

    title = re.findall(pattern, code) 
    print("%s title is : %s") % (url, title) 

答案一定是這樣的:

>>> url = raw_input('Please enter website URL : ') 
Please enter website URL : http://www.google.com/ 
>>> h = urllib.urlopen(url) >>> code = h.read() 
>>> pattern = re.compile(r'<title>(.+)</title>', re.M) 
>>> title = re.findall(pattern, code) 
>>> print("%s title is : %s") % (url, title) 
>>>output: http://www.google.com/ title is : ['Google'] 
+0

什麼是輸出Afasn? –

回答

2

在主題上面說htttp,所以看起來你只是通過增加一個額外t typo'ed http當你輸入它來測試。