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邮件的程序,这样每天可以 ...
随机推荐
- Nuxt的默认模板和默认布局
Nuxt为我们提供了超简单的默认模版订制方法,只要在根目录下创建一个app.html就可以实现了 注:建立了默认模板后,记得要重启服务器,否则你的显示不会成功 默认布局主要针对于页面的统一布局使用.它 ...
- 图片上传 一张展示,base64图片获取
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- HttpServer性能比较
在自己的本子上随便测了下几款HttpServer,环境信息就不贴出来了,主要是比对下差距. 测试内容是输出 text/plain 的 hello, world. 先说结论:Netty > Joo ...
- UVA 11464 偶数矩阵(递推 | 进制)
题目链接:https://vjudge.net/problem/UVA-11464 一道比较好的题目. 思路如下: 如果我们枚举每一个数字“变”还是“不变”,那么需要枚举$2^{255}$种情况,很显 ...
- queue 官方运用
import threading import random,time import queue q_init = queue.Queue(maxsize=5) import logging logg ...
- DIV 设置垂直居中
要说面试官经常问的问题中“如何将一个块元素水平垂直居中”就算一个. 之前的面试中也有中招,现在总结一下. 1.CSS垂直水平居中 要让DIV水平和垂直居中,必需知道该DIV得宽度和高度,然后设置位置为 ...
- Kubernetes(k8s)完整安装教程
Kubernetes(k8s)完整安装教程 2019-08-27 2.3k 记录 发表评论 目录 1 安装 1.1 安装 Docker 1.2 安装 VirtualBox 1.3 安装 kubect ...
- 17,a:img的alt和title有何异同? b:strong与en的异同?
alt(alt text):为不能显示的图像,窗体或者applets的用户代理,alt属性用来指定替换文字.替换文字的语言用lang属性来指定. eg:下例中将图像作为链接来使用 <a href ...
- 【PAT甲级】1075 PAT Judge (25 分)
题意: 输入三个正整数N,K,M(N<=10000,K<=5,M<=100000),接着输入一行K个正整数表示该题满分,接着输入M行数据,每行包括学生的ID(五位整数1~N),题号和 ...
- 【jQuery基础】
" 目录 #. 介绍 1. 优势 2. 版本 3. jQuery对象 #. 查找标签 1. 选择器 /. 基本选择器 /. 层级选择器 /. 基本筛选器 /. 使用jQuery实现弹框 / ...