博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
爬取大半导体网新闻内容保存到word(基于python3.6)
阅读量:6852 次
发布时间:2019-06-26

本文共 2030 字,大约阅读时间需要 6 分钟。

#!/usr/bin/python3 # -*- coding: utf-8 -*- # @File    : Spider # @Author  : moucong # @Date    : 2018/12/25 16:36 # @Software: PyCharm
from urllib import request from bs4 import BeautifulSoup from urllib.parse import quote from docx.shared import Inches from docx.oxml.ns import qn import string import time import re import docx import os
def spider():     url = "http://www.semi.org.cn/news/news_show.aspx?ID=54725&classid=128"     main_url = "http://www.semi.org.cn" page = request.urlopen(url).read().decode('utf-8') # html = page.read().decode('utf-8') soup = BeautifulSoup(page, "lxml") title = soup.title.string title = title.replace('\\n', '').replace('\\t', '').replace('\\r', '').replace("_SEMI大半导体产业网", '') patt = re.compile(r'

(.*?)

|', re.S) #寻找img和p标签 group = patt.findall(page) content_list = str(group[0]).split("
") file = docx.Document() for count in range(len(content_list)): x = 0 if "img" in content_list[count]: path = "E:/SEMI_job/SEMI_Spider/pic/" if not os.path.isdir(path): os.makedirs(path) paths = path + '\\' pic = re.compile('src="(.*?)"') pic_img = content_list[count] pic_url = pic.findall(pic_img) picurl = main_url+str(pic_url[0]) if ' ' in picurl: picurl = replace(picurl) picurl = quote(picurl, safe=string.printable) pic_path = "E:/SEMI_job/SEMI_Spider/pic/%s.jpg" % x pic = request.urlretrieve(picurl, pic_path) x = x+1 file.add_picture(pic_path, width=Inches(3.0)) elif "strong" in content_list[count]: strong_font = re.compile('(.*?)') strong_type = strong_font.findall(content_list[count]) p = file.add_paragraph() run = p.add_run(strong_type) # 加粗 run.font.bold = True # print(strong_type) else: file.styles['Normal'].font.name = u'宋体' file.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体') #处理word里的字体样式 content_part = content_list[count].replace('\\r', '').replace('\\n', '').replace('\\t', '') file.add_paragraph(content_part) # print(content_part) file.save("E:\SEMI_job\SEMI_Spider\writeResult.docx") print("已处理好!")

转载于:https://www.cnblogs.com/setname/p/10195397.html

你可能感兴趣的文章
Webrtc入门——基于阿里云ubuntu 最新webrtc Android平台编译详细说明
查看>>
prepareCall方法执行存储过程
查看>>
深入学习jQuery节点关系
查看>>
在浏览器中输入网址后的流程
查看>>
鼠标移动效果
查看>>
源码-hadoop1.1.0-core-org.apache.hadoop.classification
查看>>
创建4个线程,两个对j加一,两个对j减一(j两同两内)
查看>>
Make body have 100% of the browser height
查看>>
linux 服务器安装php5.6
查看>>
python 函数,闭包
查看>>
组合数据类型练习,英文词频统计实例上
查看>>
CentOS开启FTP及配置用户
查看>>
[LeetCode] Remove Duplicates from Sorted Array II
查看>>
【深度学习笔记1】如何建立和确定模型正确性?如何优化模型?
查看>>
Collection集合家族
查看>>
RtlWerpReportException failed with status code :-1073741823
查看>>
5-2 类型转换 @SuppressWarnings("unchecked")
查看>>
实验 5 编写、调试具有多个段的程序
查看>>
Verilog代码可移植性设计(转自特权同学博客http://bbs.ednchina.com/BLOG_ARTICLE_1983188.HTM)...
查看>>
浅析Linux网络子系统(三)
查看>>