Python:找到<title>

0 html python string

我有这个:

response = urllib2.urlopen(url)
html     = response.read()

begin = html.find('<title>')
end   = html.find('</title>',begin)
title = html[begin+len('<title>'):end].strip()
Run Code Online (Sandbox Code Playgroud)

如果url = http://www.google.com则标题没有"Google"的问题,

但如果url =" http://www.britishcouncil.org/learning-english-gateway "则标题成为

"<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<base href="http://www.britishcouncil.org/" />
<META http-equiv="Content-Type" Content="text/html;charset=utf-8">
<meta name="WT.sp" content="Learning;Home Page Smart View" />
<meta name="WT.cg_n" content="Learn English Gateway" />
<META NAME="DCS.dcsuri" CONTENT="/learning-english-gateway.htm">..."
Run Code Online (Sandbox Code Playgroud)

实际发生了什么,为什么我不能回归"头衔"?

Mar*_*tos 7

该URL返回一个文档与<TITLE>...</TITLE>和find区分大小写.我强烈建议您使用像Beautiful Soup这样的HTML解析器.