简介
今天试着用ptyhon做了一个抓取网页内容,并生成word文档的功能,功能很简单,做一下记录以备以后用到。
生成word用到了第三方组件python-docx,所以先进行第三方组件的安装。由于windows下安装的python默认不带setuptools这个模块,所以要先安装setuptools这个模块。
安装
1、在python官网上找到 https://bootstrap.pypa.io/ez_setup.py ,把代码保存到本地并执行: python ez_setup.py
2、下载python-docx (https://pypi.python.org/pypi/python-docx/0.7.4),下载完成后解压并进入到 XXX\python-docx-0.7.4 安装python-docx : python setup.py install
这样python-docx就安装成功了,可以用它来操作word文档了,word文档的生成参考的这里https://python-docx.readthedocs.org/en/latest/index.html
html解析用到的是sgmllib里的SGMLParser url内容的获取用到的是urllib、urllib2
实现代码
# -*- coding: cp936 -*- from sgmllib import SGMLParser import os import sys import urllib import urllib2 from docx import Document from docx.shared import Inches import time ##获取要解析的url class GetUrl(SGMLParser): def __init__(self): SGMLParser.__init__(self) self.start=False self.urlArr=[] def start_div(self,attr): for name,value in attr: if value=="ChairmanCont Bureau":#页面js中的固定值 self.start=True def end_div(self): self.start=False def start_a(self,attr): if self.start: for name,value in attr: self.urlArr.append(value) def getUrlArr(self): return self.urlArr ##解析上面获取的url,获取有用数据 class getManInfo(SGMLParser): def __init__(self): SGMLParser.__init__(self) self.start=False self.p=False self.dl=False self.manInfo=[] self.subInfo=[] def start_div(self,attr): for name,value in attr: if value=="SpeakerInfo":#页面js中的固定值 self.start=True def end_div(self): self.start=False def start_p(self,attr): if self.dl: self.p=True def end_p(self): self.p=False def start_img(self,attr): if self.dl: for name,value in attr: self.subInfo.append(value) def handle_data(self,data): if self.p: self.subInfo.append(data.decode('utf-8')) def start_dl(self,attr): if self.start: self.dl=True def end_dl(self): self.manInfo.append(self.subInfo) self.subInfo=[] self.dl=False def getManInfo(self): return self.manInfo urlSource="http://www.XXX" sourceData=urllib2.urlopen(urlSource).read() startTime=time.clock() ##get urls getUrl=GetUrl() getUrl.feed(sourceData) urlArr=getUrl.getUrlArr() getUrl.close() print "get url use:" + str((time.clock() - startTime)) startTime=time.clock() ##get maninfos manInfos=getManInfo() for url in urlArr:#one url one person data=urllib2.urlopen(url).read() manInfos.feed(data) infos=manInfos.getManInfo() manInfos.close() print "get maninfos use:" + str((time.clock() - startTime)) startTime=time.clock() #word saveFile=os.getcwd()+"\\xxx.docx" doc=Document() ##word title doc.add_heading("HEAD".decode('gbk'),0) p=doc.add_paragraph("HEADCONTENT:".decode('gbk')) ##write info for infoArr in infos: i=0 for info in infoArr: if i==0:##img url arr1=info.split('.') suffix=arr1[len(arr1)-1] arr2=info.split('/') preffix=arr2[len(arr2)-2] imgFile=os.getcwd()+"\\imgs\\"+preffix+"."+suffix if not os.path.exists(os.getcwd()+"\\imgs"): os.mkdir(os.getcwd()+"\\imgs") imgData=urllib2.urlopen(info).read() try: f=open(imgFile,'wb') f.write(imgData) f.close() doc.add_picture(imgFile,width=Inches(1.25)) os.remove(imgFile) except Exception as err: print (err) elif i==1: doc.add_heading(info+":",level=1) else: doc.add_paragraph(info,style='ListBullet') i=i+1 doc.save(saveFile) print "word use:" + str((time.clock() - startTime))
总结
以上就是本文关于python解析html提取数据,并生成word文档实例解析的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。