Python 爬取 热词并进行分类数据分析-[拓扑数据]
日期:2020.01.29
博客期:137
星期三
【本博客的代码如若要使用,请在下方评论区留言,之后再用(就是跟我说一声)】
所有相关跳转:
a.【简单准备】
b.【云图制作+数据导入】
c.【拓扑数据】(本期博客)
d.【数据修复】
e.【解释修复+热词引用】
f.【JSP演示+页面跳转】
g.【热词分类+目录生成】
h.【热词关系图+报告生成】
i . 【App制作】
j . 【安全性改造】
嗯,先声明一下 “拓扑数据”的意思,应老师需求,我们需要将热词的解释、引用等数据从百科网站中爬取下来,之后将统一的热词数据进行文件处理,组合成新的数据表,然后可以在网页上(暂时是网页)展示更多的信息。
嗯,可以对热词解释进行爬取了,给大家看一下 (以人工智能为例) 
我发现了一个问题:
setAttr("value","人工智能")方法并不能实现input的value属性值变为想要的“人工智能”,我采用的是sendKeys("人工智能")方法来实现,不过这样又有了一个问题,每一次sendKeys()相当于再input内部又附加了这样的字符,比如原本input里有“茄子”字样,之后使用sendKeys(“蔬菜”),input里就变成了“茄子蔬菜”!这个问题就导致了我不能实现页面直接跳转。如何解决呢?
我从它的方法里找到了clear()方法,亲测可用(在sendKeys之前使用)。
我在这里提供测试类代码:
import parsel
from urllib import request
import codecs
from selenium import webdriver
import time # [ 对字符串的特殊处理方法-集合 ]
class StrSpecialDealer:
# 取得当前标签内的文本
@staticmethod
def getReaction(stri):
strs = StrSpecialDealer.simpleDeal(str(stri))
strs = strs[strs.find('>')+1:strs.rfind('<')]
return strs # 去除基本的分隔符
@staticmethod
def simpleDeal(stri):
strs = str(stri).replace(" ", "")
strs = strs.replace("\t", "")
strs = strs.replace("\r", "")
strs = strs.replace("\n", "")
return strs # 删除所有标签标记
@staticmethod
def deleteRe(stri):
strs = str(stri)
st = strs.find('<')
while(st!=-1):
str_delete = strs[strs.find('<'):strs.find('>')+1]
strs = strs.replace(str_delete,"")
st = strs.find('<') return strs # 删除带有 日期 的句子
@staticmethod
def de_date(stri):
lines = str(stri).split("。")
strs = ""
num = lines.__len__()
for i in range(0,num):
st = str(lines[i])
if (st.__contains__("年") | st.__contains__("月")):
pass
else:
strs += st + "。"
strs = strs.replace("。。", "。")
return strs # 取得带有 日期 的句子之前的句子
@staticmethod
def ut_date(stri):
lines = str(stri).split("。")
strs = ""
num = lines.__len__()
for i in range(0, num):
st = str(lines[i])
if (st.__contains__("年")| st.__contains__("月")):
break
else:
strs += st + "。"
strs = strs.replace("。。","。")
return strs @staticmethod
def beat(stri,num):
strs = str(stri)
for i in range(0,num):
strs = strs.replace("["+str(i)+"]","") return strs # [ 连续网页爬取的对象 ]
class WebConnector:
profile = ""
sw = "" # ---[定义构造方法]
def __init__(self):
self.profile = webdriver.Firefox()
self.profile.get('https://baike.baidu.com/') # ---[定义释放方法]
def __close__(self):
self.profile.quit() # 获取 url 的内部 HTML 代码
def getHTMLText(self):
a = self.profile.page_source
return a # 获取页面内的基本链接
def getFirstChanel(self):
index_html = self.getHTMLText()
index_sel = parsel.Selector(index_html)
links = index_sel.css('.lemma-summary').extract()[0]
tpl = StrSpecialDealer.simpleDeal(str(links))
tpl = StrSpecialDealer.beat(tpl,20)
tpl = StrSpecialDealer.deleteRe(tpl)
tpl = StrSpecialDealer.ut_date(tpl)
return tpl def getMore(self,refers):
self.profile.find_element_by_id("query").clear()
self.profile.find_element_by_id("query").send_keys(refers)
self.profile.find_element_by_id("search").click()
time.sleep(1) def main():
wc = WebConnector()
wc.getMore("人工智能")
s = wc.getFirstChanel()
print(s)
wc.getMore("5G")
t = wc.getFirstChanel()
print(t)
wc.__close__() main()
test.py
嗯,然后我继续整合,将数据导入成文件批处理
对应代码:
import parsel
from urllib import request
import codecs
from selenium import webdriver
import time # [ 整理后的数据 ]
class Info: # ---[ 方法区 ]
# 构造方法
def __init__(self,name,num,more):
self.name = name
self.num = num
self.more = more def __toString__(self):
return (self.name+"\t"+str(self.num)+"\t"+self.more) def __toSql__(self,table):
return ("Insert into "+table+" values ('"+self.name+"',"+self.num+",'"+self.more+"');") # ---[ 数据区 ]
# 名称
name = ""
# 频数
num = 0
# 中文解释
more = 0 # [写文件的方法集合]
class FileToWebAndContent: fileReaderPath = ""
wc = ""
sw = "" def __init__(self,r,w):
self.fileReaderPath = r
self.wc = WebConnector()
self.sw = StringWriter(w)
self.sw.makeFileNull() def __free__(self):
self.wc.__close__() def __deal__(self):
fw = open(self.fileReaderPath, mode='r', encoding='utf-8')
lines = fw.readlines()
num = lines.__len__()
for i in range(0,num):
str_line = lines[i]
gr = str_line.split("\t")
name_b = StrSpecialDealer.simpleDeal(gr[0])
num_b = StrSpecialDealer.simpleDeal(gr[1])
if(int(num_b)<=2):
break
self.wc.getMore(name_b)
more_b = self.wc.getFirstChanel()
if(more_b==""):
continue
info = Info(name_b,num_b,more_b)
self.sw.write(info.__toString__()) # [ 对字符串的特殊处理方法-集合 ]
class StrSpecialDealer:
# 取得当前标签内的文本
@staticmethod
def getReaction(stri):
strs = StrSpecialDealer.simpleDeal(str(stri))
strs = strs[strs.find('>')+1:strs.rfind('<')]
return strs # 去除基本的分隔符
@staticmethod
def simpleDeal(stri):
strs = str(stri).replace(" ", "")
strs = strs.replace("\t", "")
strs = strs.replace("\r", "")
strs = strs.replace("\n", "")
return strs # 删除所有标签标记
@staticmethod
def deleteRe(stri):
strs = str(stri)
st = strs.find('<')
while(st!=-1):
str_delete = strs[strs.find('<'):strs.find('>')+1]
strs = strs.replace(str_delete,"")
st = strs.find('<') return strs # 删除带有 日期 的句子
@staticmethod
def de_date(stri):
lines = str(stri).split("。")
strs = ""
num = lines.__len__()
for i in range(0,num):
st = str(lines[i])
if (st.__contains__("年") | st.__contains__("月")):
pass
else:
strs += st + "。"
strs = strs.replace("。。", "。")
return strs # 取得带有 日期 的句子之前的句子
@staticmethod
def ut_date(stri):
lines = str(stri).split("。")
strs = ""
num = lines.__len__()
for i in range(0, num):
st = str(lines[i])
if (st.__contains__("年")| st.__contains__("月")):
break
else:
strs += st + "。"
strs = strs.replace("。。","。")
return strs @staticmethod
def beat(stri,num):
strs = str(stri)
for i in range(0,num):
strs = strs.replace("["+str(i)+"]","") return strs # [写文件的方法集合]
class StringWriter:
filePath = "" def __init__(self,str):
self.filePath = str
pass def makeFileNull(self):
f = codecs.open(self.filePath, "w+", 'utf-8')
f.write("")
f.close() def write(self,stri):
f = codecs.open(self.filePath, "a+", 'utf-8')
f.write(stri + "\n")
f.close() # [ 连续网页爬取的对象 ]
class WebConnector:
profile = ""
sw = "" # ---[定义构造方法]
def __init__(self):
self.profile = webdriver.Firefox()
self.profile.get('https://baike.baidu.com/')
# self.sw = StringWriter("../testFile/rc/moreinfo.txt")
# self.sw.makeFileNull() # ---[定义释放方法]
def __close__(self):
self.profile.quit() # 获取 url 的内部 HTML 代码
def getHTMLText(self):
a = self.profile.page_source
return a # 获取页面内的基本链接
def getFirstChanel(self):
try:
index_html = self.getHTMLText()
index_sel = parsel.Selector(index_html)
links = index_sel.css('.lemma-summary').extract()[0]
tpl = StrSpecialDealer.simpleDeal(str(links))
tpl = StrSpecialDealer.beat(tpl, 20)
tpl = StrSpecialDealer.deleteRe(tpl)
tpl = StrSpecialDealer.ut_date(tpl)
return tpl
except:
return "" def getMore(self,refers):
self.profile.find_element_by_id("query").clear()
self.profile.find_element_by_id("query").send_keys(refers)
self.profile.find_element_by_id("search").click()
time.sleep(1) def main():
ftwac = FileToWebAndContent("../testFile/rc/output.txt", "../testFile/rc/moreinfo.txt")
ftwac.__deal__()
ftwac.__free__() main()
MoreInfo.py
对应得到文件截图:
Python 爬取 热词并进行分类数据分析-[拓扑数据]的更多相关文章
- Python 爬取 热词并进行分类数据分析-[解释修复+热词引用]
日期:2020.02.02 博客期:141 星期日 [本博客的代码如若要使用,请在下方评论区留言,之后再用(就是跟我说一声)] 所有相关跳转: a.[简单准备] b.[云图制作+数据导入] c.[拓扑 ...
- Python 爬取 热词并进行分类数据分析-[热词分类+目录生成]
日期:2020.02.04 博客期:143 星期二 [本博客的代码如若要使用,请在下方评论区留言,之后再用(就是跟我说一声)] 所有相关跳转: a.[简单准备] b.[云图制作+数据导入] c.[ ...
- Python 爬取 热词并进行分类数据分析-[云图制作+数据导入]
日期:2020.01.28 博客期:136 星期二 [本博客的代码如若要使用,请在下方评论区留言,之后再用(就是跟我说一声)] 所有相关跳转: a.[简单准备] b.[云图制作+数据导入](本期博客) ...
- Python 爬取 热词并进行分类数据分析-[简单准备] (2020年寒假小目标05)
日期:2020.01.27 博客期:135 星期一 [本博客的代码如若要使用,请在下方评论区留言,之后再用(就是跟我说一声)] 所有相关跳转: a.[简单准备](本期博客) b.[云图制作+数据导入] ...
- Python 爬取 热词并进行分类数据分析-[数据修复]
日期:2020.02.01 博客期:140 星期六 [本博客的代码如若要使用,请在下方评论区留言,之后再用(就是跟我说一声)] 所有相关跳转: a.[简单准备] b.[云图制作+数据导入] c.[拓扑 ...
- Python 爬取 热词并进行分类数据分析-[App制作]
日期:2020.02.14 博客期:154 星期五 [本博客的代码如若要使用,请在下方评论区留言,之后再用(就是跟我说一声)] 所有相关跳转: a.[简单准备] b.[云图制作+数据导入] c.[拓扑 ...
- Python 爬取 热词并进行分类数据分析-[JSP演示+页面跳转]
日期:2020.02.03 博客期:142 星期一 [本博客的代码如若要使用,请在下方评论区留言,之后再用(就是跟我说一声)] 所有相关跳转: a.[简单准备] b.[云图制作+数据导入] c.[拓扑 ...
- Python 爬取 热词并进行分类数据分析-[热词关系图+报告生成]
日期:2020.02.05 博客期:144 星期三 [本博客的代码如若要使用,请在下方评论区留言,之后再用(就是跟我说一声)] 所有相关跳转: a.[简单准备] b.[云图制作+数据导入] c.[拓扑 ...
- Python爬取热搜存入数据库并且还能定时发送邮件!!!
一.前言 微博热搜榜每天都会更新一些新鲜事,但是自己处于各种原因,肯定不能时刻关注着微博,为了与时代接轨,接受最新资讯,就寻思着用Python写个定时爬取微博热搜的并且发送QQ邮件的程序,这样每天可以 ...
随机推荐
- buuctf 二维码
首先下载文件 然后用解压工具解压之后 发现是一个二维码 扫描二维码 并没有拿到 flag 然后将图片拖进 hxd中搜索PK发现有一个压缩包 将压缩包提取出来 暴力破解 然后得到密码 然后解压 然后得 ...
- Windows引用opencv静态库
参考博客:https://www.cnblogs.com/sysuzyq/p/6183568.html
- A task in a suit and a tie:paraphrase generation with semantic augmentation解读
1.该算法核心:在seq2seq模型的编码器中增加语义的frame 和 roles 2.上图为算法整个流程: 1).首先输入一句话s,SLING会使用frame和role label注释输入语句s,然 ...
- ALSA driver--PCM实例创建框架
在介绍PCM 之前,我们先给出创建PCM实例的框架. #include <sound/pcm.h> .... /* hardware definition */ static struct ...
- WKWebView 使用的坑
WKWebView 简介: WKWebView 是苹果在 WWDC 2014 上推出的新一代 webView 组件,用以替代 UIKit 中笨重难用.内存泄漏的 UIWebView.WKWebView ...
- idea选中文件时左侧菜单自动定位到文件所在位置
一:设置成自动定位,如图: 二:点击项目工程目录上的阻击按钮,自动定位
- PP Bottle Have High Cycle Times
This year, the participation of 0.1% -0.4% sorbitol nucleating agent in general PP can produce high- ...
- 外置ADC
美信关于如何简化微控制器与温度传感器的接口设计?: 一般外置ADC与单片机UC之间通过SPI或SMBUS接口通信 当IO口比较紧张时可以选择脉冲或频率方波正比与测量值输出的外置ADC,此时也可以实现光 ...
- C++文件写入,读出函数ofstream,ifstream的使用方法
ofstream是从内存到硬盘,ifstream是从硬盘到内存,其实所谓的流缓冲就是内存空间. 1.插入器(<<) 向流输出数据.比如说系统有一个默认的标准输出流(cout),一般情况下 ...
- mongodb 用户指引
维护人:陈权 一.mongodb install on linuxcurl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.6 ...