-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']
什麼是輸出Afasn? –