这是我的代码,我想从用户输入的URL中提取Web的'title',但它行不通。
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']
在主题中说htttp
,因此看起来您只需添加额外的 t
即可输入 http
。