简介
今天试着用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文档实例解析的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新动态
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]