python解析html提取数据,并生成word文档实例解析
今天试着用ptyhon做了一个抓取网页内容,并生成word文档的功能,功能很简单,做一下记录以备以后用到。
生成word用到了第三方组件python-docx,所以先进行第三方组件的安装。由于windows下安装的python默认不带setuptools这个模块,所以要先安装setuptools这个模块。
1、在python官网上找到https://bootstrap.pypa.io/ez_setup.py,把代码保存到本地并执行:pythonez_setup.py
2、下载python-docx(https://pypi.python.org/pypi/python-docx/0.7.4),下载完成后解压并进入到XXX\python-docx-0.7.4安装python-docx:pythonsetup.pyinstall
这样python-docx就安装成功了,可以用它来操作word文档了,word文档的生成参考的这里https://python-docx.readthedocs.org/en/latest/index.html
html解析用到的是sgmllib里的SGMLParserurl内容的获取用到的是urllib、urllib2
#-*-coding:cp936-*-
fromsgmllibimportSGMLParser
importos
importsys
importurllib
importurllib2
fromdocximportDocument
fromdocx.sharedimportInches
importtime
##获取要解析的url
classGetUrl(SGMLParser):
def__init__(self):
SGMLParser.__init__(self)
self.start=False
self.urlArr=[]
defstart_div(self,attr):
forname,valueinattr:
ifvalue=="ChairmanContBureau":#页面js中的固定值
self.start=True
defend_div(self):
self.start=False
defstart_a(self,attr):
ifself.start:
forname,valueinattr:
self.urlArr.append(value)
defgetUrlArr(self):
returnself.urlArr
##解析上面获取的url,获取有用数据
classgetManInfo(SGMLParser):
def__init__(self):
SGMLParser.__init__(self)
self.start=False
self.p=False
self.dl=False
self.manInfo=[]
self.subInfo=[]
defstart_div(self,attr):
forname,valueinattr:
ifvalue=="SpeakerInfo":#页面js中的固定值
self.start=True
defend_div(self):
self.start=False
defstart_p(self,attr):
ifself.dl:
self.p=True
defend_p(self):
self.p=False
defstart_img(self,attr):
ifself.dl:
forname,valueinattr:
self.subInfo.append(value)
defhandle_data(self,data):
ifself.p:
self.subInfo.append(data.decode('utf-8'))
defstart_dl(self,attr):
ifself.start:
self.dl=True
defend_dl(self):
self.manInfo.append(self.subInfo)
self.subInfo=[]
self.dl=False
defgetManInfo(self):
returnself.manInfo
urlSource="http://www.XXX"
sourceData=urllib2.urlopen(urlSource).read()
startTime=time.clock()
##geturls
getUrl=GetUrl()
getUrl.feed(sourceData)
urlArr=getUrl.getUrlArr()
getUrl.close()
print"geturluse:"+str((time.clock()-startTime))
startTime=time.clock()
##getmaninfos
manInfos=getManInfo()
forurlinurlArr:#oneurloneperson
data=urllib2.urlopen(url).read()
manInfos.feed(data)
infos=manInfos.getManInfo()
manInfos.close()
print"getmaninfosuse:"+str((time.clock()-startTime))
startTime=time.clock()
#word
saveFile=os.getcwd()+"\\xxx.docx"
doc=Document()
##wordtitle
doc.add_heading("HEAD".decode('gbk'),0)
p=doc.add_paragraph("HEADCONTENT:".decode('gbk'))
##writeinfo
forinfoArrininfos:
i=0
forinfoininfoArr:
ifi==0:##imgurl
arr1=info.split('.')
suffix=arr1[len(arr1)-1]
arr2=info.split('/')
preffix=arr2[len(arr2)-2]
imgFile=os.getcwd()+"\\imgs\\"+preffix+"."+suffix
ifnotos.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)
exceptExceptionaserr:
print(err)
elifi==1:
doc.add_heading(info+":",level=1)
else:
doc.add_paragraph(info,style='ListBullet')
i=i+1
doc.save(saveFile)
print"worduse:"+str((time.clock()-startTime))
以上就是本文关于python解析html提取数据,并生成word文档实例解析的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!