如何使用Python检索网页的页面标题(标题html标签)?
Answers:
我将始终将lxml用于此类任务。您也可以使用beautifulsoup。
import lxml.html
t = lxml.html.parse(url)
print t.find(".//title").text
根据评论进行编辑:
from urllib2 import urlopen
from lxml.html import parse
url = "https://www.google.com"
page = urlopen(url)
p = parse(page)
print p.find(".//title").text
这是@Vinko Vrsalovic的答案的简化版本:
import urllib2
from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup(urllib2.urlopen("https://www.google.com"))
print soup.title.string
注意:
soup.title在html文档中的任何位置找到第一个title元素
title.string假定它只有一个子节点,并且该子节点是一个字符串
对于beautifulsoup 4.x,请使用不同的导入:
from bs4 import BeautifulSoup
urlllib.request代替urllib2。不知道为什么。为了避免有关解析器的BeautifulSoup警告,我必须这样做soup = BeautifulSoup(urllib.request.urlopen(url), "lxml")。
import urllib.request as urllib代替import urllib2
<title></title>执行中的空标题soup.title.string将返回None
无需导入其他库。请求具有内置的此功能。
>> hearders = {'headers':'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:51.0) Gecko/20100101 Firefox/51.0'}
>>> n = requests.get('http://www.imdb.com/title/tt0108778/', headers=hearders)
>>> al = n.text
>>> al[al.find('<title>') + 7 : al.find('</title>')]
u'Friends (TV Series 1994\u20132004) - IMDb'
对于这样一个简单的任务,这可能是过高的,但是如果您打算做更多的事情,那么从这些工具(机械化,BeautifulSoup)开始比较明智,因为它们比其他工具(使用urllib获取内容和进行正则表达式)更容易使用或其他解析器来解析html)
链接: BeautifulSoup 机械化
#!/usr/bin/env python
#coding:utf-8
from BeautifulSoup import BeautifulSoup
from mechanize import Browser
#This retrieves the webpage content
br = Browser()
res = br.open("https://www.google.com/")
data = res.get_data()
#This parses the content
soup = BeautifulSoup(data)
title = soup.find('title')
#This outputs the content :)
print title.renderContents()
使用HTMLParser:
from urllib.request import urlopen
from html.parser import HTMLParser
class TitleParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.match = False
self.title = ''
def handle_starttag(self, tag, attributes):
self.match = tag == 'title'
def handle_data(self, data):
if self.match:
self.title = data
self.match = False
url = "http://example.com/"
html_string = str(urlopen(url).read())
parser = TitleParser()
parser.feed(html_string)
print(parser.title) # prints: Example Domain
r=urlopen(url),encoding = r.info().get_content_charset(),和html_string = r.read().decode(encoding)。
使用正则表达式
import re
match = re.search('<title>(.*?)</title>', raw_html)
title = match.group(1) if match else 'No title'
soup.title.string实际上返回一个unicode字符串。要将其转换为普通字符串,您需要
string=string.encode('ascii','ignore')
encode给出的内容)而不是字符串,请使用正确的编码charset。例如string.encode('utf-8')。
这是一个容错HTMLParser实现。
您可以扔很多东西get_title()而不会破坏它,如果发生任何意外情况,
get_title()将返回None。
当Parser()下载它,它编码的页面ASCII
,无论在忽略任何错误的页面使用的字符集的。进行更改to_ascii()以将数据转换为UTF-8或任何其他编码将是微不足道的。只需添加一个编码参数并将函数重命名为即可to_encoding()。
默认情况下,HTMLParser()它将在损坏的html上中断,甚至在不匹配的标记(例如不匹配的标记)上中断。为了防止这种行为,我将HTMLParser()的错误方法替换为将忽略错误的函数。
#-*-coding:utf8;-*-
#qpy:3
#qpy:console
'''
Extract the title from a web page using
the standard lib.
'''
from html.parser import HTMLParser
from urllib.request import urlopen
import urllib
def error_callback(*_, **__):
pass
def is_string(data):
return isinstance(data, str)
def is_bytes(data):
return isinstance(data, bytes)
def to_ascii(data):
if is_string(data):
data = data.encode('ascii', errors='ignore')
elif is_bytes(data):
data = data.decode('ascii', errors='ignore')
else:
data = str(data).encode('ascii', errors='ignore')
return data
class Parser(HTMLParser):
def __init__(self, url):
self.title = None
self.rec = False
HTMLParser.__init__(self)
try:
self.feed(to_ascii(urlopen(url).read()))
except urllib.error.HTTPError:
return
except urllib.error.URLError:
return
except ValueError:
return
self.rec = False
self.error = error_callback
def handle_starttag(self, tag, attrs):
if tag == 'title':
self.rec = True
def handle_data(self, data):
if self.rec:
self.title = data
def handle_endtag(self, tag):
if tag == 'title':
self.rec = False
def get_title(url):
return Parser(url).title
print(get_title('http://www.google.com'))