python解析html提取數(shù)據(jù),并生成word文檔實(shí)例解析
簡(jiǎn)介
今天試著用ptyhon做了一個(gè)抓取網(wǎng)頁(yè)內(nèi)容,并生成word文檔的功能,功能很簡(jiǎn)單,做一下記錄以備以后用到。
生成word用到了第三方組件python-docx,所以先進(jìn)行第三方組件的安裝。由于windows下安裝的python默認(rèn)不帶setuptools這個(gè)模塊,所以要先安裝setuptools這個(gè)模塊。
安裝
1、在python官網(wǎng)上找到 https://bootstrap.pypa.io/ez_setup.py ,把代碼保存到本地并執(zhí)行: python ez_setup.py
2、下載python-docx (https://pypi.python.org/pypi/python-docx/0.7.4),下載完成后解壓并進(jìn)入到 XXX\python-docx-0.7.4 安裝python-docx : python setup.py install
這樣python-docx就安裝成功了,可以用它來(lái)操作word文檔了,word文檔的生成參考的這里https://python-docx.readthedocs.org/en/latest/index.html
html解析用到的是sgmllib里的SGMLParser url內(nèi)容的獲取用到的是urllib、urllib2
實(shí)現(xiàn)代碼
# -*- 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":#頁(yè)面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,獲取有用數(shù)據(jù)
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":#頁(yè)面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))
總結(jié)
以上就是本文關(guān)于python解析html提取數(shù)據(jù),并生成word文檔實(shí)例解析的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專(zhuān)題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
相關(guān)文章
Python機(jī)器學(xué)習(xí)之決策樹(shù)和隨機(jī)森林
本文主要介紹了機(jī)器學(xué)習(xí)之決策樹(shù)和隨機(jī)森林,詳細(xì)的介紹了實(shí)現(xiàn) 原理機(jī)器實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
Pyqt5 實(shí)現(xiàn)窗口縮放,控件在窗口內(nèi)自動(dòng)伸縮的操作
這篇文章主要介紹了Pyqt5 實(shí)現(xiàn)窗口縮放,控件在窗口內(nèi)自動(dòng)伸縮的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03
Python PyCharm如何進(jìn)行斷點(diǎn)調(diào)試
這篇文章主要介紹了Python PyCharm如何進(jìn)行斷點(diǎn)調(diào)試,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
如何解決pycharm中用matplotlib畫(huà)圖不顯示中文的問(wèn)題
這篇文章主要介紹了如何解決pycharm中用matplotlib畫(huà)圖不顯示中文的問(wèn)題,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-06-06
Python并行庫(kù)joblib之delayed函數(shù)與Parallel函數(shù)詳解
這篇文章主要介紹了Python并行庫(kù)joblib之delayed函數(shù)與Parallel函數(shù)詳解,Joblib就是一個(gè)可以簡(jiǎn)單地將Python代碼轉(zhuǎn)換為并行計(jì)算模式的軟件包,它可非常簡(jiǎn)單并行我們的程序,從而提高計(jì)算速度,需要的朋友可以參考下2023-08-08
Python + Flask 實(shí)現(xiàn)簡(jiǎn)單的驗(yàn)證碼系統(tǒng)
這篇文章主要介紹了Python + Flask 制作一個(gè)簡(jiǎn)單的驗(yàn)證碼系統(tǒng),本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10
Python中selenium實(shí)現(xiàn)文件上傳所有方法整理總結(jié)
本篇文章主要介紹了Python中selenium實(shí)現(xiàn)文件上傳所有方法整理總結(jié),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-04-04
實(shí)例解析Python設(shè)計(jì)模式編程之橋接模式的運(yùn)用
這篇文章主要介紹了Python設(shè)計(jì)模式編程之橋接模式的運(yùn)用,橋接模式主張把抽象部分與它的實(shí)現(xiàn)部分分離,需要的朋友可以參考下2016-03-03

