Python 爬取 热词并进行分类数据分析-[简单准备] (2020年寒假小目标05)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python 爬取 热词并进行分类数据分析-[简单准备] (2020年寒假小目标05),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2207字,纯文字阅读大概需要4分钟。
内容图文
![Python 爬取 热词并进行分类数据分析-[简单准备] (2020年寒假小目标05)](/upload/InfoBanner/zyjiaocheng/1191/d5d977b8193143c9a5630c2ed9c3e13e.jpg)
日期:2020.01.27
博客期:135
星期一
【本博客的代码如若要使用,请在下方评论区留言,之后再用(就是跟我说一声)】
今天问了一下老师,信息领域热词从哪里爬,老师说是IT方面的新闻,嗯~有点儿意思了!
我找到了好多IT网站,但是大多数广告又多,名词也不专一针对信息领域,所以啊我就暂且用例一个相对还好的例子:
数据来源网址:https://news.51cto.com/(最终不一定使用此网站的爬取数据)
网站的相关热词来源截图:
如图,“智能”、“技术”、“区块链”为爬取目标
进行爬取(因为每一次执行js都会加重爬取任务的负担),当你执行到第100次的时候,你现在要执行第101次的JS,它所消耗的时间大概是27s!所以,这种方法我就爬100次,得到5607条数据:
爬取代码:
![技术分享图片](/img/jia.gif)
![技术分享图片](/img/jian.gif)
1 import parsel 2 from urllib import request 3import codecs 4from selenium import webdriver 5import time 6 7# [ 对字符串的特殊处理方法-集合 ] 8class StrSpecialDealer: 9 @staticmethod 10def getReaction(stri): 11 strs = str(stri).replace("","") 12 strs = strs[strs.find(‘>‘)+1:strs.rfind(‘<‘)] 13 strs = strs.replace("\t","") 14 strs = strs.replace("\r","") 15 strs = strs.replace("\n","") 16return strs 1718class StringWriter: 19 filePath = ""20def__init__(self,str): 21 self.filePath = str 22pass2324def makeFileNull(self): 25 f = codecs.open(self.filePath, "w+", ‘utf-8‘) 26 f.write("") 27 f.close() 2829def write(self,stri): 30 f = codecs.open(self.filePath, "a+", ‘utf-8‘) 31 f.write(stri + "\n") 32 f.close() 333435# [ 连续网页爬取的对象 ]36class WebConnector: 37 profile = ""38 sw = ""39# ---[定义构造方法]40def__init__(self): 41 self.profile = webdriver.Firefox() 42 self.profile.get(‘https://news.51cto.com/‘) 43 self.sw = StringWriter("../testFile/info.txt") 44 self.sw.makeFileNull() 4546# ---[定义释放方法]47def__close__(self): 48 self.profile.quit() 4950# 获取 url 的内部 HTML 代码51def getHTMLText(self): 52 a = self.profile.page_source 53return a 5455# 获取页面内的基本链接56def getFirstChanel(self): 57 index_html = self.getHTMLText() 58 index_sel = parsel.Selector(index_html) 59 links = index_sel.css(‘.tag‘).extract() 60 num = links.__len__() 61print("Len="+str(num)) 62for i in range(0,num): 63 tpl = StrSpecialDealer.getReaction(links[i]) 64 self.sw.write(tpl) 6566def getMore(self): 67 self.profile.find_element_by_css_selector(".listsmore").click() 68 time.sleep(1) 6970def main(): 71 wc = WebConnector() 72for i in range(0,100): 73print(i) 74 wc.getMore() 75 wc.getFirstChanel() 76 wc.__close__() 777879 main()
之后再使用MapReduce进行次数统计,就可以了(还可以配合维基百科和百度百科获取(爬取)相关热词的其他信息)
原文:https://www.cnblogs.com/onepersonwholive/p/12236198.html
内容总结
以上是互联网集市为您收集整理的Python 爬取 热词并进行分类数据分析-[简单准备] (2020年寒假小目标05)全部内容,希望文章能够帮你解决Python 爬取 热词并进行分类数据分析-[简单准备] (2020年寒假小目标05)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。