日期: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 爬取 热词并进行分类数据分析-[拓扑数据]的更多相关文章

  1. Python 爬取 热词并进行分类数据分析-[解释修复+热词引用]

    日期:2020.02.02 博客期:141 星期日 [本博客的代码如若要使用,请在下方评论区留言,之后再用(就是跟我说一声)] 所有相关跳转: a.[简单准备] b.[云图制作+数据导入] c.[拓扑 ...

  2. Python 爬取 热词并进行分类数据分析-[热词分类+目录生成]

    日期:2020.02.04 博客期:143 星期二   [本博客的代码如若要使用,请在下方评论区留言,之后再用(就是跟我说一声)] 所有相关跳转: a.[简单准备] b.[云图制作+数据导入] c.[ ...

  3. Python 爬取 热词并进行分类数据分析-[云图制作+数据导入]

    日期:2020.01.28 博客期:136 星期二 [本博客的代码如若要使用,请在下方评论区留言,之后再用(就是跟我说一声)] 所有相关跳转: a.[简单准备] b.[云图制作+数据导入](本期博客) ...

  4. Python 爬取 热词并进行分类数据分析-[简单准备] (2020年寒假小目标05)

    日期:2020.01.27 博客期:135 星期一 [本博客的代码如若要使用,请在下方评论区留言,之后再用(就是跟我说一声)] 所有相关跳转: a.[简单准备](本期博客) b.[云图制作+数据导入] ...

  5. Python 爬取 热词并进行分类数据分析-[数据修复]

    日期:2020.02.01 博客期:140 星期六 [本博客的代码如若要使用,请在下方评论区留言,之后再用(就是跟我说一声)] 所有相关跳转: a.[简单准备] b.[云图制作+数据导入] c.[拓扑 ...

  6. Python 爬取 热词并进行分类数据分析-[App制作]

    日期:2020.02.14 博客期:154 星期五 [本博客的代码如若要使用,请在下方评论区留言,之后再用(就是跟我说一声)] 所有相关跳转: a.[简单准备] b.[云图制作+数据导入] c.[拓扑 ...

  7. Python 爬取 热词并进行分类数据分析-[JSP演示+页面跳转]

    日期:2020.02.03 博客期:142 星期一 [本博客的代码如若要使用,请在下方评论区留言,之后再用(就是跟我说一声)] 所有相关跳转: a.[简单准备] b.[云图制作+数据导入] c.[拓扑 ...

  8. Python 爬取 热词并进行分类数据分析-[热词关系图+报告生成]

    日期:2020.02.05 博客期:144 星期三 [本博客的代码如若要使用,请在下方评论区留言,之后再用(就是跟我说一声)] 所有相关跳转: a.[简单准备] b.[云图制作+数据导入] c.[拓扑 ...

  9. Python爬取热搜存入数据库并且还能定时发送邮件!!!

    一.前言 微博热搜榜每天都会更新一些新鲜事,但是自己处于各种原因,肯定不能时刻关注着微博,为了与时代接轨,接受最新资讯,就寻思着用Python写个定时爬取微博热搜的并且发送QQ邮件的程序,这样每天可以 ...

随机推荐

  1. 【应急响应】Linux安全加固

    一.补丁管理 1.查看系统信息 uname -a 2.配置yun源 CentosOS 可以直接升级 RHEL系列可以配置使用CentosOS源 3.升级软件包 yum –y update 二.安全工具 ...

  2. KFC 小猪短租

    # 分析肯德基门店信息 import requests,json post_url = 'http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op= ...

  3. js的基本分类(0.1)

    js分内嵌,内联,外部样式表三种, js的组成分,ES(变量,函数,对象,数组,判断,循环),BOM(浏览器对象模型),DOM(文档对象模型), js的五种输出语句,alert(警告框),confir ...

  4. 部署DVWA时的一些问题和解决办法(一)

    第一个有可能遇到的问题 0x4 配置PHP 配置PHP,GD支持 系统从2017更新到2018多个php版本共存问题解决,phpinfo 显示7.0 ,而php -v 显示7.2问题 apt-get ...

  5. (原创)SoapUI学习(2)- POST请求

    1.新建Project,右键Projects->New REST Project,可以不填直接点击OK,之后通过rename重命名.(如果这里添加URL,则直接达到第三步的图) 2.右键新建的工 ...

  6. Python隐藏特性:字符串驻留、常量折叠

    下面是Python字符串的一些微妙的特性,绝对会让你大吃一惊. 案例一: >>> a = "some_string" >>> id(a) 140 ...

  7. 获取表格数据转换为JSON字符串

    核心代码JavaScript代码: 方法一 function sc () { var myTable=document.getElementById("myTable"); //获 ...

  8. RS422接口与RS485接口

    RS422具体接线参考网站 RS485接口 RS485设备为半双工设备,RS485收发器信号相关引脚包括控制引脚.485A.485B,其中控制引脚的高低电平决定当前处于接收模式还是发送模式. RS48 ...

  9. tarjan-无向图(求割点)

    一.基本概念 1.割点:无向连通图中,如果删除某点后,图变成不连通,则称改点为割点. 2.桥:无向连通图中,如果去掉某条边后,整张无向图会分成两部分(即整张图不连通),这样的一条边成为桥. 3.点双连 ...

  10. Go 后端主要做什么

    漫谈 Go 语言后端开发 :https://blog.csdn.net/u010986776/article/details/87276303 Golang 资深后端工程师要了解的知识:https:/ ...