天在处理html数据的时候发现了python里面比较好玩的几个库,先存起来之后有时间慢慢再去学习和使用,觉得是一件蛮有意思的事情。今天想学习使用的是html2text模块和readability模块。其中,第一个模块是负责对html数据进行处理的,返回html中的文本信息;第二个模块是负责html数据中指定信息如:文章标题、作者等信息的提取。
之前在处理爬取的html数据的时候大多数是自己编写正则表达式或者是xpath规则集来完成指定数据字段信息的提取,相对来说比较灵活,但是规则集的编写较为耗时,且遇上复杂数据的时候就难以应付了。如果有现成的封装好的模块可以直接完成某一项工作的话还是很不错的,这两个库可以说还是比较不错,今天只是简单拿来使用一下,之后再做到类似的项目的时候可以将一部分的工作交由固定模块来完成。
下面是具体的实践:
#!usr/bin/env python # encoding:utf-8 ''' __Author__:沂水寒城 功能: html2text 模块和 readability 模块使用 ''' import sys import urllib import requests import html2text from readability import Document reload(sys) sys.setdefaultencoding('utf-8') def test_func(): ''' 官网实例,清洗html ''' print html2text.html2text("<p>Hello, world.</p>") h=html2text.HTML2Text() h.ignore_links=True print h.handle("<p>Hello, <a href='http://earth.google.com/'>world</a>!") def test_func2(url): ''' 获取指定URL的html,对html进行处理 ''' html=urllib.urlopen(url).read() h=html2text.HTML2Text() h.ignore_links=True print h.handle(html) def test_func3(url): ''' 抽取指定URL中的标题等数据 ''' response=requests.get(url) doc=Document(response.text) print doc.title() html=urllib.urlopen(url).read() #该方式抽取出来的readable_article是带HTML标签的文本 readable_article=Document(html).summary() readable_title=Document(html).short_title() print 'readable_article: ',readable_article print 'readable_title: ',readable_title if __name__=='__main__': url='https://mbd.baidu.com/newspage/data/landingsuper?context=%7B%22nid%22%3A%22news_2588586383061242738%22%7D&n_type=0&p_from=1' test_func() print '-|'*50 test_func2(url) print '-|'*50 test_func3(url)
运行结果如下:
所周知,python最强大的地方在于,python社区汇总拥有丰富的第三方库,开源的特性,使得有越来越多的技术开发者来完善。
python的完美性。
未来人工智能,大数据方向,区块链的识别和进阶都将以python为中心来展开。
咳咳咳! 好像有点打广告的嫌疑了。
当前互联网信息共享时代,最重要的是什么?是数据。最有价值的是什么?是数据。最能直观体现技术水平的是什么?还是数据。
所以,今天我们要分享的是:如何来获取各个文件格式的文本信息。
普通文件的格式 一般分为: txt普通文本信息,doc word文档,html网页内容,excel表格数据,以及特殊的mht文件。
一、Python处理html网页信息
html类型的文本数据,内容是由前端代码书写的标签+文本数据的格式,可以直接在chrome浏览器打开,清楚 的展示出文本的格式。
python 获取html文件的内容和获取txt文件的方法相同,直接打开文件读取就可以了。
读取代码如下:
with open(html_path, "r", encoding="utf-8") as f: file = f.read()
file 是html文件的文本内容。是一个网页标签的格式内容。
二、Python处理excel表格信息
python拥有直接操作excel表格的第三方库xlwt,xlrd。调用对应的方法就可以读写excel表格数据。
读取excel操作代码如下:
filepath = "C:\\Users\Administrator\Desktop\新建文件夹\笨笨 前程6份 武汉.xls" sheet_name = "UserList" rb = xlrd.open_workbook(filepath) sheet = rb.sheet_by_name(sheet_name) # clox_list = [0, 9, 14, 15, 17] for row in range(1, sheet.nrows): w = WriteToExcel() # for clox in clox_list: name = sheet.cell(row, 0).value phone = sheet.cell(row, 15).value address = sheet.cell(row, 9).value major = sheet.cell(row, 14).value age = sheet.cell(row, 8).value
其中row是表格数据对应的行数, cell获取具体行数,列数的具体数据。
三、Python读取doc文档数据
python读取doc文档是最麻烦的。处理逻辑复杂。处理的方式也有很多种。
python 没有直接处理doc文档的第三方库,但是有一个处理docx的第三方库。可以通过将doc文件转换为docx文件,再调用第三方python库pydocx来读取doc文档的内容。
这里需要注意的是,不要直接修改doc的后缀来修改成docx文件。直接通过修改后缀获取的docx文件,pydocx无法读取内容。
我们可以使用另外一个库来修改doc为docx。
具体代码如下:
def doSaveAas(self, doc_path): """ 将doc文档转换为docx文档 :rtype: object """ docx_path = doc_path.replace("doc", "docx") word = wc.Dispatch('Word.Application') doc = word.Documents.Open(doc_path) # 目标路径下的文件 doc.SaveAs(docx_path, 12, False, "", True, "", False, False, False, False) # 转化后路径下的文件 doc.Close() word.Quit()
代码所需的包接口:
import os import zipfile from win32com import client as wc import xlrd from bs4 import BeautifulSoup from pydocx import PyDocX from lxml import html from xpath_content import XpathContent from write_to_excel import WriteToExcel
python处理docx文档的方法有很多种,具体使用情况,根据个人需求来决定。
No.1 解压docx文件
docx文件的原理,本质上就是一个压缩的zip文件,通过解压以后,就可以获取原来文件的各个内容。
docx解压后的文件结构如下:
docx文件的文本内容存储结构如下:
文本内容存储于word/document.xml文件中。
第一种方法,我们就可以先将docx还原成zip压缩文件,再解压zip文件,读取word/document.xml文件的内容就ok了。
具体操作代码如下:
def get_content(self): """ 获取docx文档的文本内容 :rtype: object """ os.chdir(r"C:\Users\Administrator\Desktop\新建文件夹") # 改变目录到文件的目录 # os.rename("51 2014.09.12 1份Savannah.docx", "51 2014.09.12 1份Savannah.ZIP") # 重命名为zip文件 f = zipfile.ZipFile('51 2014.09.12 1份Savannah.ZIP', 'r') # 进行解压 xml = f.read("word/document.xml") wordObj = BeautifulSoup(xml.decode("utf-8")) # print(wordObj) texts = wordObj.findAll("w:t") content = [] for text in texts: content.append(text.text) content_str = "".join(content) return content_str
最后获取到的就是docx文档的所有文本数据了。
No.2 将docx文档转换成python能够处理的文本格式
第一种方法,是依据docx文档的原理来获取数据,流程有点繁琐,有没有能直接读取docx文档内容的方法呢?答案,肯定是没有的,别想了,洗洗回家睡吧。
直接读取docx文档的方法没有,有没有能够将docx文档转换成python能够轻松处理的文本格式呢?
这个可以有,前面说了,python拥有大量丰富的第三方库(先夸一波我大python),历经千辛万苦终于找到了,一个能转换docx文档格式的第三方库,pydocx,pydocx库中有个方法pydocx.to_html()就可以直接将docx文档转换为html文件,怎么样?意不意外,惊喜不惊喜!
第二种方法,转换文本格式的代码如下:
def docx_to_html(self, docx_path): """ docx文档转换成html响应 :rtype: object """ # docx_path = "C:\\Users\Administrator\Desktop\新建文件夹\\51 2014.09.12 1份Savannah.docx" response = PyDocX.to_html(docx_path)
获取到的response是html文件内容。
四、Python处理mht文件
mht文件是一种只能在IE浏览器上展示的文本格式,在chrome浏览器中打开是一堆的乱码。
No.1 伪造IE请求mht文件内容
最基础的读取mht文本的方法就是伪造IE浏览器请求。
调用requests库,发送get请求网页链接,构造IE的请求头信息。
理论上来说,这种方法是可行的。但是呢,不建议用,原因大家都懂得。
No.2 转换文件格式
好了说正经的方法,猜测mht文件能否修改成其他文件格式来直接读取呢?
docx,不行;html,不行;excel,更不用说了。
真相只有一个!!!
直接修改后缀得到的docx,无法读取。
so,我们想到的方法是什么呢。没错,就是修改成doc文档。
方法是匪夷所思的,但也是灵感一现。
mht可以直接通过修改后缀转换成doc文档,doc文档读取文本内容的方法具体参考上面读取doc文档的方法。
如何获取html文本的内容?
html文本的内容是网页结构标签数据,取出文本的方式是:re正则,或者xpath。
后续,小伙伴有需要的话,会再开一章详细了解re,xapth的使用规则。
来源网络,侵权联系删除
颜色属性被用来设置文字的颜色。
颜色是通过CSS最经常的指定:
一个网页的文本颜色是指在主体内的选择:
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=640, user-scalable=no">
<title>项目</title>
<style>
body {
color: blue;
}
h1 {
color: #00ff00;
}
h2 {
color: rgb(255, 0, 0);
}
</style>
</head>
<body>
<h2>hello world</h2>
<h1>welcome to CaoZhou</h1>
</body>
</html>
注:对于W3C标准的CSS:如果你定义了颜色属性,你还必须定义背景色属性。
文本排列属性是用来设置文本的水平对齐方式。
文本可居中或对齐到左或右,两端对齐。
当text-align设置为"justify",每一行被展开为宽度相等,左,右外边距是对齐(如杂志和报纸)。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
h1 {
text-align: center;
}
p.date {
text-align: right;
}
p.main {
text-align: justify;
}
</style>
</head>
<body>
<p class="date">2015 年 3 月 14 号</p>
<p class="main"> 从前有个书生,和未婚妻约好在某年某月某日结婚。到那一天,未婚妻却嫁给了别人。书生受此打击, 一病不起。 这时,路过一游方僧人,从怀里摸出一面镜子叫书生看。书生看到茫茫大海,一名遇害的女子一丝不挂地躺在海滩上。路过一人, 看一眼,摇摇头,走了。又路过一人,将衣服脱下,给女尸盖上,走了。再路过一人,过去,挖个坑,小心翼翼把尸体掩埋了。 僧人解释道, 那具海滩上的女尸,就是你未婚妻的前世。你是第二个路过的人,曾给过他一件衣服。她今生和你相恋,只为还你一个情。但是她最终要报答一生一世的人,是最后那个把她掩埋的人,那人就是他现在的丈夫。书生大悟,病愈。
</p>
<p><b>注意:</b> 重置浏览器窗口大小查看 "justify" 是如何工作的。</p>
</body>
</html>
text-decoration 属性用来设置或删除文本的装饰。
从设计的角度看 text-decoration属性主要是用来删除链接的下划线:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.none {}
.del {
text-decoration: none;
}
</style>
</head>
<body>
<p>原来的样子</p>
<a href="#" class="none">wwwwwwwwwwwwwwwwww</a>
<p>去掉下划线</p>
<a href="#" class="del">wwwwwwwwwwwwwwwwwwwww</a>
</body>
</html>
也可以这样装饰文字:
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=640, user-scalable=no">
<title>项目</title>
<style>
h1 {
text-decoration: overline;
}
h2 {
text-decoration: line-through;
}
h3 {
text-decoration: underline;
}
</style>
</head>
<body>
<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<h3>This is heading 3</h3>
</body>
</html>
注:不建议强调指出不是链接的文本,因为这常常混淆用户。
text-transform文本转换属性是用来指定在一个文本中的大写和小写字母。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=640, user-scalable=no">
<title>项目</title>
<style>
p.uppercase {
text-transform: uppercase;
}
p.lowercase {
text-transform: lowercase;
}
p.capitalize {
text-transform: capitalize;
}
</style>
</head>
<body>
<p class="uppercase">This is some text.</p>
<p class="lowercase">This is some text.</p>
<p class="capitalize">This is some text.</p>
</body>
</html>
text-indent文本缩进属性是用来指定文本的第一行的缩进。
p {text-indent:50px;}
增加或减少字符之间的空间。
<style>
h1 {
letter-spacing:2px;
}
h2 {
letter-spacing:-3px;
}
</style>
指定在一个段落中行之间的空间。
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=640, user-scalable=no">
<title>项目</title>
<style>
p.small {
line-height: 70%;
}
p.big {
line-height: 200%;
}
</style>
</head>
<body>
<p>
This is a paragraph with a standard line-height.<br> This is a paragraph with a standard line-height.<br> The default line height in most browsers is about 110% to 120%.<br>
</p>
<p class="small">
This is a paragraph with a smaller line-height.<br> This is a paragraph with a smaller line-height.<br> This is a paragraph with a smaller line-height.<br> This is a paragraph with a smaller line-height.<br>
</p>
<p class="big">
This is a paragraph with a bigger line-height.<br> This is a paragraph with a bigger line-height.<br> This is a paragraph with a bigger line-height.<br> This is a paragraph with a bigger line-height.<br>
</p>
</body>
</html>
增加一个段落中的单词之间的空白空间。
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=640, user-scalable=no">
<title>项目</title>
<style type="text/css">
p {
word-spacing: 30px;
}
</style>
</head>
<body>
<p>
This is some text. This is some text.
</p>
</body>
</html>
设置文本的垂直对齐图像。
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=640, user-scalable=no">
<title>项目</title>
<style>
img{
width: 200px;
height: 100px;
}
img.top {
vertical-align: text-top;
}
img.bottom {
vertical-align: text-bottom;
}
</style>
</head>
<body>
<p>An <img src="img/logo.png" /> image with a default alignment.</p>
<p>An <img class="top" src="img/logo.png" /> image with a text-top alignment.</p>
<p>An <img class="bottom" src="img/logo.png" /> image with a text-bottom alignment.</p>
</body>
</html>
设置文本阴影。
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=640, user-scalable=no">
<title>项目</title>
<style>
h1{
text-shadow: 2px 2px #FF0000;
}
</style>
</head>
<body>
<h1>Text-shadow effect</h1>
</body>
</html>
本文主要介绍了CSS文本样式实际应用中应该如何去操作,通过讲解文本中对应的属性去改变文本的表现形式。使用丰富的效果图的展示,能够更直观的看到运行的效果,能够更好的理解。使用Html语言,代码结构更佳的清晰,能够帮助你更好的学习。
*请认真填写需求信息,我们会在24小时内与您取得联系。