python抽取指定url页面的title方法


Posted in Python onMay 11, 2018

今天简单使用了一下python的re模块和lxml模块,分别利用的它们提供的正则表达式和xpath来解析页面源码从中提取所需的title,xpath在完成这样的小任务上效率非常好,在这里之所以又使用了一下正则表达式是因为xpath在处理一些特殊的页面的时候会出现乱码的情况,当然这不是xpath的原因,而是页面本身编码,跟utf-8转码之间有冲突所致,这里看代码:

# !/usr/bin/python
#-*-coding:utf-8-*-
'''
功能:抽取指定url的页面内容中的title
'''
import re
import chardet
import urllib
from lxml import etree
def 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 strs
def 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))
 title = page.xpath('/html/head/title/text()')
 try:
  title = title[0].strip()
 except IndexError:
  print 'Nothing'
 print title
def get_title(Html):
 '''
 用re抽取网页Title
 '''
 Html = utf8_transfer(Html)
 compile_rule = ur'<title>.*</title>'
 title_list = re.findall(compile_rule, Html)
 if title_list == []:
  title = ''
 else:
  title = title_list[0][7:-8]
 print title
if __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方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中关于使用模块的基础知识
May 24 Python
python避免死锁方法实例分析
Jun 04 Python
python奇偶行分开存储实现代码
Mar 19 Python
Python Flask框架扩展操作示例
May 03 Python
python gdal安装与简单使用
Aug 01 Python
numpy np.newaxis 的实用分享
Nov 30 Python
python sorted函数原理解析及练习
Feb 10 Python
Windows 下python3.8环境安装教程图文详解
Mar 11 Python
详解python中groupby函数通俗易懂
May 14 Python
python实现sm2和sm4国密(国家商用密码)算法的示例
Sep 26 Python
在Windows下安装配置CPU版的PyTorch的方法
Apr 02 Python
Python尝试实现蒙特卡罗模拟期权定价
Apr 21 Python
python清除字符串中间空格的实例讲解
May 11 #Python
Python3利用Dlib19.7实现摄像头人脸识别的方法
May 11 #Python
Python中的TCP socket写法示例
May 11 #Python
Python简单定义与使用二叉树示例
May 11 #Python
Python堆排序原理与实现方法详解
May 11 #Python
python 执行shell命令并将结果保存的实例
May 11 #Python
python 实现登录网页的操作方法
May 11 #Python
You might like
对text数据类型不支持代码页转换 从: 1252 到: 936
2011/04/23 PHP
php中可能用来加密字符串的函数[base64_encode、urlencode、sha1]
2012/01/16 PHP
Yii2 ActiveRecord多表关联及多表关联搜索的实现
2016/06/30 PHP
浅谈PHP中静态方法和非静态方法的相互调用
2016/10/04 PHP
PHP通过文件保存和更新信息的方法分析
2019/09/12 PHP
firefox浏览器下javascript 拖动层效果与原理分析代码
2007/12/04 Javascript
js正确获取元素样式详解
2009/08/07 Javascript
JavaScript实现单击下拉框选择直接跳转页面的方法
2015/07/02 Javascript
JavaScript基于原型链的继承
2016/06/22 Javascript
极力推荐10个短小实用的JavaScript代码段
2016/08/03 Javascript
基于JavaScript实现熔岩灯效果导航菜单
2017/01/04 Javascript
Angular.js 4.x中表单Template-Driven Forms详解
2017/04/25 Javascript
基于 Bootstrap Datetimepicker 联动
2017/08/03 Javascript
解决Vue打包后访问图片/图标不显示的问题
2019/07/25 Javascript
基于ajax及jQuery实现局部刷新过程解析
2020/09/12 jQuery
[54:47]Liquid vs VP Supermajor决赛 BO 第五场 6.10
2018/07/05 DOTA
详解Python发送email的三种方式
2018/10/18 Python
用Python识别人脸,人种等各种信息
2019/07/15 Python
python django生成迁移文件的实例
2019/08/31 Python
Python任务调度模块APScheduler使用
2020/04/15 Python
python 进程池pool使用详解
2020/10/15 Python
关于python3.9安装wordcloud出错的问题及解决办法
2020/11/02 Python
matplotlib bar()实现百分比堆积柱状图
2021/02/24 Python
比利时网上药店: Drogisterij.net
2017/03/17 全球购物
新加坡网上花店:FlowerAdvisor新加坡
2018/10/05 全球购物
碧欧泉Biotherm加拿大官方网站:法国高端护肤品牌
2019/10/18 全球购物
C/C++有关内存的思考题
2015/12/04 面试题
简单叙述一下MYSQL的优化
2016/05/09 面试题
大专生的学习自我评价
2013/12/04 职场文书
医生爱岗敬业演讲稿
2014/08/26 职场文书
报表员工作失误检讨书范文
2014/09/19 职场文书
优秀教师先进事迹材料
2014/12/15 职场文书
优秀教师推荐材料
2014/12/16 职场文书
现实表现材料范文
2014/12/23 职场文书
2015年全国助残日活动方案
2015/05/04 职场文书
php 防护xss,PHP的防御XSS注入的终极解决方案
2021/04/01 PHP