python抽取指定url页面的title方法
今天简单使用了一下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_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抽取指定url页面的title方法的更多相关文章
- Python+Selenium学习--打印当前页面的title及url
场景 测试中,访问1个页面然后判断其title是否符合预期是很常见的1个用例,所谓用例不够,title来凑就是这个道理.更具体一点,假设1个页面的title应该是'hello world', 那么可以 ...
- 转载: js jquery 获取当前页面的url,获取frameset中指定的页面的url(有修改)
转载网址:http://blog.csdn.net/bestlxm/article/details/6800077 js jquery 怎么获取当前页面的url,获取frameset中指定的页面的ur ...
- Vue设置页面的title
原文地址:http://www.cnblogs.com/JimmyBright/p/7410771.html 前端框架如Vue.React等都是单页面的应用,也就是说整个web站点其实都是一个inde ...
- JS魔法堂:定义页面的Dispose方法——[before]unload事件启示录
前言 最近实施的同事报障,说用户审批流程后直接关闭浏览器,操作十余次后系统就报用户会话数超过上限,咨询4A同事后得知登陆后需要显式调用登出API才能清理4A端,否则必然会超出会话上限. 即使在页面 ...
- 定义页面的Dispose方法:[before]unload事件启示录
前言 最近实施的同事报障,说用户审批流程后直接关闭浏览器,操作十余次后系统就报用户会话数超过上限,咨询4A同事后得知登陆后需要显式调用登出API才能清理4A端,否则必然会超出会话上限. 即使在页面上增 ...
- 提交(post)xml文件给指定url的2种方法
原文:提交(post)xml文件给指定url的2种方法 1 这段代码是在网上搜到的,拿来共享,项目正好要用到.其中的data你只需要传递一个xml字符串就可以 protected string ...
- 使用vue-router设置每个页面的title
进入 router 文件夹底下的index.js文件 首先引入: import Vue from 'vue' import Router from 'vue-router' 然后在路由里面配置每个路由 ...
- 获取页面的title值
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 调用上一个页面的js方法
点击商品分类,弹出下框 点击确定,将选中的类别的name和唯一的code返回到上个页面 function save(){ var ids = getIdSelections(); jp.get(&qu ...
随机推荐
- 早上好,我是 Istio 1.1
1性能增强 虽然Istio1.0的目标是生产可用,但从去年7月份发布以来,在性能和稳定性上并不能让用户满意.社区的Performance and Scalability工作组在Istio v1.1中做 ...
- Mysql读写分离 及高可用高性能负载均衡实现
什么是读写分离,说白了就是mysql服务器读的操作和写的操作是分开的,当然这个需要两台服务器,master负责写,slave负责读,当然我们可以使用多个slave,这样我们也实现了简单意义上的高可用和 ...
- sql语句中的占位符?有什么作用
String sql = "SELECT userid,name FROM tuser WHERE userid=? AND password=?" ; pstmt = conn. ...
- Longest Common Subsequence (DP)
Given two strings, find the longest common subsequence (LCS). Your code should return the length of ...
- 不一样的 Null
前不久处理一个异常的时候发现了一段有趣的代码,一同事在往表里(Sql Server 数据库)添加数据的时候给可以为 null 的字段赋了如下的值: Student stu = new Student( ...
- 数据结构实验之图论一:基于邻接矩阵的广度优先搜索遍历 (SDUT 2141)
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> ...
- NSArray 的创建和遍历
数组 用来存贮对象的有序列表,它是不可变的 不能存数C语言的基本数据类型 只支持OC对象 #pragma mark Create a array //Initialize NSArray void a ...
- codeforces#1215E. Marbles(状压dp)
题目链接: http://codeforces.com/contest/1215/problem/E 题意: 至少多少次操作可以使得相同的数都是相邻的 每次操作可以交换两个相邻的数 数据范围: $1\ ...
- elasticsearch shield(5.0以下版本 权限认证)
elasticsearch 5.0以下的版本要用到权限控制的话需要使用shield.下载地址: https://www.elastic.co/downloads/shield5.0以上的版本则可以使用 ...
- C#程序调用CMD执行命令,将参数传递给cmd.exe
proc.StartInfo.Arguments = "/c ping 10.2.2.125"; C#程序调用CMD执行命令 将参数传递给cmd.exe的(Passing an a ...