今天簡單使用了一下python的re模塊和lxml模塊,分別利用的它們提供的正則表達式和xpath來解析頁面源碼從中提取所需的title,xpath在完成這樣的小任務上效率非常好,在這里之所以又使用了一下正則表達式是因為xpath在處理一些特殊的頁面的時候會出現亂碼的情況,當然這不是xpath的原因,而是頁面本身編碼,跟utf-8轉碼之間有沖突所致,這里看代碼:
# !/usr/bin/python#-*-coding:utf-8-*-'''功能:抽取指定url的頁面內容中的title'''import reimport chardetimport urllibfrom lxml import etreedef utf8_transfer(strs): ''' utf8編碼轉換 ''' try:  if isinstance(strs, unicode):   strs = strs.encode('utf-8')  elif chardet.detect(strs)['encoding'] == 'GB2312':   strs = strs.decode("gb2312", 'ignore').encode('utf-8')  elif chardet.detect(strs)['encoding'] == 'utf-8':   strs = strs.decode('utf-8', 'ignore').encode('utf-8') except Exception, e:  print 'utf8_transfer error', strs, e return strsdef get_title_xpath(Html): ''' 用xpath抽取網頁Title ''' Html = utf8_transfer(Html) Html_encoding = chardet.detect(Html)['encoding'] page = etree.HTML(Html, parser=etree.HTMLParser(encoding=Html_encoding)) try: except IndexError:  print 'Nothing' print titledef get_title(Html): ''' 用re抽取網頁Title ''' Html = utf8_transfer(Html) compile_rule = ur'<title>.*</title>' title_list = re.findall(compile_rule, Html) if title_list == []: else: print titleif __name__ == '__main__':	url = 'http://www.baidu.com'	html = urllib.urlopen(url).read()	new_html = utf8_transfer(html)	try:		get_title_xpath(new_html)		get_title(new_html)	except Exception, e:		print e下面是結果:
	百度一下,你就知道
	百度一下,你就知道
簡單的小實踐,繼續學習,歡迎交流。
以上這篇python抽取指定url頁面的title方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答