人工智能的训练需要样本,大量采集样本单靠人力是不现实的,一是利用规则生成带标签的数据,二就是在网上电脑自动获取了

今天我就来小试一下

目录

爬取热搜数据

1.找到数据位置

2.爬取网页

1.初试

2.模拟登陆

3.爬取网页

3.提取数据

2.词云

1.数据处理

2.生成词云

2.展示图片


爬取热搜数据

1.找到数据位置

我们首先打开微博热搜,使用开发者工具(F12)后按ctrl+shift+c 来inspect热搜内容。

右边拷贝selector为下代码段内容,可以看到热搜内容是挂载在tr结点上的,我们可以用beautifulsoup,来对该结点进行遍历。

#pl_top_realtimehot > table > tbody > tr:nth-child(14) > td.td-02

2.爬取网页

1.初试

第一次使用了 requests来爬取

注意:需要更改一下编码模式,默认编码模式有乱码。

import requests
hot_url = 'https://s.weibo.com/top/summary/?cate=realtimehot'
r = requests.get(hot_url)
r.encoding = 'gbk'
r=r.text
print(r)

尴尬的是,微博有反爬虫机制,好像无法处理跳转的网页,出现了下述情况。

<!DOCTYPE html>
<html>
<head><meta http-equiv="Content-type" content="text/html; charset=gb2312"/><title>Sina Visitor System</title>
</head>
<body>
<span id="message"></span>
<script type="text/javascript" src="/js/visitor/mini_original.js?v=20161116"></script>
<script type="text/javascript">window.use_fp = "1" == "1"; // 是否采集设备指纹。var url = url || {};(function () {this.l = function (u, c) {try {var s = document.createElement("script");s.type = "text/javascript";s[document.all ? "onreadystatechange" : "onload"] = function () {if (document.all && this.readyState != "loaded" && this.readyState != "complete") {return}this[document.all ? "onreadystatechange" : "onload"] = null;this.parentNode.removeChild(this);if (c) {c()}};s.src = u;document.getElementsByTagName("head")[0].appendChild(s)} catch (e) {}};}).call(url);// 流程入口。wload(function () {try {var need_restore = "1" == "1"; // 是否走恢复身份流程。// 如果需要走恢复身份流程,尝试从 cookie 获取用户身份。if (!need_restore || !Store.CookieHelper.get("SRF")) {// 若获取失败走创建访客流程。// 流程执行时间过长(超过 3s),则认为出错。var error_timeout = window.setTimeout("error_back()", 5000);tid.get(function (tid, where, confidence) {// 取指纹顺利完成,清除出错 timeout 。window.clearTimeout(error_timeout);incarnate(tid, where, confidence);});} else {// 用户身份存在,尝试恢复用户身份。restore();}} catch (e) {// 出错。error_back();}});// “返回” 回调函数。var return_back = function (response) {if (response["retcode"] == 20000000) {back();} else {// 出错。error_back(response["msg"]);}};// 跳转回初始地址。var back = function() {var url = "https://s.weibo.com/top/summary/?cate=realtimehot";if (url != "none") {window.location.href = url;}};// 跨域广播。var cross_domain = function (response) {var from = "weibo";var entry = "miniblog";if (response["retcode"] == 20000000) {var crossdomain_host = "login.sina.com.cn";if (crossdomain_host != "none") {var cross_domain_intr = window.location.protocol + "//" + crossdomain_host + "/visitor/visitor?a=crossdomain&cb=return_back&s=" +encodeURIComponent(response["data"]["sub"]) + "&sp=" + encodeURIComponent(response["data"]["subp"]) + "&from=" + from + "&_rand=" + Math.random() + "&entry=" + entry;url.l(cross_domain_intr);} else {back();}} else {// 出错。error_back(response["msg"]);}};// 为用户赋予访客身份 。var incarnate = function (tid, where, conficence) {var gen_conf = "";var from = "weibo";var incarnate_intr = window.location.protocol + "//" + window.location.host + "/visitor/visitor?a=incarnate&t=" +encodeURIComponent(tid) + "&w=" + encodeURIComponent(where) + "&c=" + encodeURIComponent(conficence) +"&gc=" + encodeURIComponent(gen_conf) + "&cb=cross_domain&from=" + from + "&_rand=" + Math.random();url.l(incarnate_intr);};// 恢复用户丢失的身份。var restore = function () {var from = "weibo";var restore_intr = window.location.protocol + "//" + window.location.host +"/visitor/visitor?a=restore&cb=restore_back&from=" + from + "&_rand=" + Math.random();url.l(restore_intr);};// 跨域恢复丢失的身份。var restore_back = function (response) {// 身份恢复成功走广播流程,否则走创建访客流程。if (response["retcode"] == 20000000) {var url = "https://s.weibo.com/top/summary/?cate=realtimehot";var alt = response["data"]["alt"];var savestate = response["data"]["savestate"];if (alt != "") {requrl = (url == "none") ? "" : "&url=" + encodeURIComponent(url);var params = "entry=sso&alt=" + encodeURIComponent(alt) + "&returntype=META" +"&gateway=1&savestate=" + encodeURIComponent(savestate) + requrl;window.location.href = "https://login.sina.com.cn/sso/login.php?" + params;} else {cross_domain(response);}} else if(response['retcode'] == 50111261 && isInIframe()) {//do nothing} else {tid.get(function (tid, where, confidence) {incarnate(tid, where, confidence);});}};// 出错情况返回登录页。var error_back = function (msg) {var url = "https://s.weibo.com/top/summary/?cate=realtimehot";var clientType = "pc";if (url != "none") {if (url.indexOf("ssovie4c55=0") === -1) {url += (((url.indexOf("?") === -1) ? "?" : "&") + "ssovie4c55=0");}if (clientType == "mobile") {window.location.href = "https://passport.weibo.cn/signin/login?r="+url;} else{window.location.href = "https://weibo.com/login.php";}} else {if(document.getElementById("message")) {document.getElementById("message").innerHTML = "Error occurred" + (msg ? (": " + msg) : ".");}}};var isInIframe = function () {try {return window.self !== window.top;} catch (e) {return true;}};</script>
</body>
</html>

2.模拟登陆

我们直接使用selenium来预登陆,这里我们从这下载浏览器驱动到python目录,使用selenium来直接登录并获取cookie来登录

from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
driver = webdriver.Chrome()
print('准备登陆Weibo.cn网站...')
driver.get("https://login.sina.com.cn/signup/signin.php")
wait = WebDriverWait(driver, 5)
# 暂停1分钟进行预登陆,填写账号密码及验证
time.sleep(60)
cookies = driver.get_cookies()

3.爬取网页

我们利用得到的cookie登录微博,并爬取网页

s = requests.Session()
c = requests.cookies.RequestsCookieJar()
for item in cookies:c.set(item["name"],item["value"])
print(c)
s.cookies.update(c)
r = s.get('https://s.weibo.com/top/summary')
r.encoding = 'utf-8'

3.提取数据

我们用之前得到的selector来进行正则化处理

hotness = soup.select('#pl_top_realtimehot > table > tbody > tr > td.td-02 > span')

最后得到

{'title': '中国印钞造币总公司董事陈耀明主动投案', 'url': 'https://s.weibo.com/weibo?q=%23%E7%99%BE%E5%B9%B4%E9%A3%8E%E5%8D%8E%E8%B0%81%E4%B8%8E%E5%85%B1%23&Refer=new_time', 'hotness': ' 2032589'}, {'title': '江苏第一针免费HPV疫苗开打', 'url': 'https://s.weibo.com/weibo?q=%23%E4%B8%AD%E5%9B%BD%E5%8D%B0%E9%92%9E%E9%80%A0%E5%B8%81%E6%80%BB%E5%85%AC%E5%8F%B8%E8%91%A3%E4%BA%8B%E9%99%88%E8%80%80%E6%98%8E%E4%B8%BB%E5%8A%A8%E6%8A%95%E6%A1%88%23&Refer=top', 'hotness': ' 1473925'}, {'title': '立足中国大地讲好中国故事', 'url': 'https://s.weibo.com/weibo?q=%23%E6%B1%9F%E8%8B%8F%E7%AC%AC%E4%B8%80%E9%92%88%E5%85%8D%E8%B4%B9HPV%E7%96%AB%E8%8B%97%E5%BC%80%E6%89%93%23&Refer=top', 'hotness': ' 1291733'}, {'title': 'Apple京东首支功夫大片', 'url': 'https://s.weibo.com/weibo?q=%23%E7%AB%8B%E8%B6%B3%E4%B8%AD%E5%9B%BD%E5%A4%A7%E5%9C%B0%E8%AE%B2%E5%A5%BD%E4%B8%AD%E5%9B%BD%E6%95%85%E4%BA%8B%23&Refer=top', 'hotness': ' '}, {'title': '河南一彩民中526万元逾期未兑', 'url': 'https://s.weibo.comjavascript:void(0);', 'hotness': ' 1211148'}, {'title': '薇娅被撤销网络诚信宣传大使', 'url': 'https://s.weibo.com/weibo?q=%23%E6%B2%B3%E5%8D%97%E4%B8%80%E5%BD%A9%E6%B0%91%E4%B8%AD526%E4%B8%87%E5%85%83%E9%80%BE%E6%9C%9F%E6%9C%AA%E5%85%91%23&Refer=top', 'hotness': ' 1174563'}, {'title': '杜华为黄明昊包100场电影', 'url': 'https://s.weibo.com/weibo?q=%23%E8%96%87%E5%A8%85%E8%A2%AB%E6%92%A4%E9%94%80%E7%BD%91%E7%BB%9C%E8%AF%9A%E4%BF%A1%E5%AE%A3%E4%BC%A0%E5%A4%A7%E4%BD%BF%23&Refer=top', 'hotness': ' 1165657'}, {'title': '浙大学霸作息表太震撼了', 'url': 'https://s.weibo.com/weibo?q=%23%E6%9D%9C%E5%8D%8E%E4%B8%BA%E9%BB%84%E6%98%8E%E6%98%8A%E5%8C%85100%E5%9C%BA%E7%94%B5%E5%BD%B1%23&Refer=top', 'hotness': ' 1090345'}, {'title': '刘小敏陈卓恋情曝光', 'url': 'https://s.weibo.com/weibo?q=%23%E6%B5%99%E5%A4%A7%E5%AD%A6%E9%9C%B8%E4%BD%9C%E6%81%AF%E8%A1%A8%E5%A4%AA%E9%9C%87%E6%92%BC%E4%BA%86%23&Refer=top', 'hotness': ' 898496'}, {'title': '大连男子直播卖房单日成交150套', 'url': 'https://s.weibo.com/weibo?q=%23%E5%88%98%E5%B0%8F%E6%95%8F%E9%99%88%E5%8D%93%E6%81%8B%E6%83%85%E6%9B%9D%E5%85%89%23&Refer=top', 'hotness': ' 885352'}, {'title': '2岁娃误食含西布曲明减肥药离世', 'url': 'https://s.weibo.com/weibo?q=%23%E5%A4%A7%E8%BF%9E%E7%94%B7%E5%AD%90%E7%9B%B4%E6%92%AD%E5%8D%96%E6%88%BF%E5%8D%95%E6%97%A5%E6%88%90%E4%BA%A4150%E5%A5%97%23&Refer=top', 'hotness': ' 873230'}, {'title': '父亲房内睡觉4岁女童爬窗不慎坠亡', 'url': 'https://s.weibo.com/weibo?q=%232%E5%B2%81%E5%A8%83%E8%AF%AF%E9%A3%9F%E5%90%AB%E8%A5%BF%E5%B8%83%E6%9B%B2%E6%98%8E%E5%87%8F%E8%82%A5%E8%8D%AF%E7%A6%BB%E4%B8%96%23&Refer=top', 'hotness': ' 847428'}, {'title': '英特尔宣称禁用新疆产品', 'url': 'https://s.weibo.com/weibo?q=%23%E7%88%B6%E4%BA%B2%E6%88%BF%E5%86%85%E7%9D%A1%E8%A7%894%E5%B2%81%E5%A5%B3%E7%AB%A5%E7%88%AC%E7%AA%97%E4%B8%8D%E6%85%8E%E5%9D%A0%E4%BA%A1%23&Refer=top', 'hotness': ' 841085'}, {'title': '戚薇工作室发文回应', 'url': 'https://s.weibo.com/weibo?q=%23%E8%8B%B1%E7%89%B9%E5%B0%94%E5%AE%A3%E7%A7%B0%E7%A6%81%E7%94%A8%E6%96%B0%E7%96%86%E4%BA%A7%E5%93%81%23&Refer=top', 'hotness': ' 825458'}, {'title': '因疫情无法到西安考研怎么办', 'url': 'https://s.weibo.com/weibo?q=%23%E6%88%9A%E8%96%87%E5%B7%A5%E4%BD%9C%E5%AE%A4%E5%8F%91%E6%96%87%E5%9B%9E%E5%BA%94%23&Refer=top', 'hotness': ' 813324'}, {'title': '李佳琦声音商标再被驳回', 'url': 'https://s.weibo.com/weibo?q=%23%E5%9B%A0%E7%96%AB%E6%83%85%E6%97%A0%E6%B3%95%E5%88%B0%E8%A5%BF%E5%AE%89%E8%80%83%E7%A0%94%E6%80%8E%E4%B9%88%E5%8A%9E%23&Refer=top', 'hotness': ' 785381'}, {'title': '李佳琦直播', 'url': 'https://s.weibo.com/weibo?q=%23%E6%9D%8E%E4%BD%B3%E7%90%A6%E5%A3%B0%E9%9F%B3%E5%95%86%E6%A0%87%E5%86%8D%E8%A2%AB%E9%A9%B3%E5%9B%9E%23&Refer=top', 'hotness': ' 777485'}, {'title': '冬至', 'url': 'https://s.weibo.com/weibo?q=%23%E6%9D%8E%E4%BD%B3%E7%90%A6%E7%9B%B4%E6%92%AD%23&Refer=top', 'hotness': ' 760975'}, {'title': '初一女孩把数学卷答成语文卷', 'url': 'https://s.weibo.com/weibo?q=%E5%86%AC%E8%87%B3&Refer=top', 'hotness': ' 676459'}, {'title': '圣诞美甲', 'url': 'https://s.weibo.com/weibo?q=%23%E5%88%9D%E4%B8%80%E5%A5%B3%E5%AD%A9%E6%8A%8A%E6%95%B0%E5%AD%A6%E5%8D%B7%E7%AD%94%E6%88%90%E8%AF%AD%E6%96%87%E5%8D%B7%23&Refer=top', 'hotness': ' 641451'}, {'title': '腾讯要求抖音为斗罗大陆赔偿8亿', 'url': 'https://s.weibo.com/weibo?q=%E5%9C%A3%E8%AF%9E%E7%BE%8E%E7%94%B2&Refer=top', 'hotness': ' 530197'}, {'title': '河南一饺子店连续6年冬至停业', 'url': 'https://s.weibo.com/weibo?q=%23%E8%85%BE%E8%AE%AF%E8%A6%81%E6%B1%82%E6%8A%96%E9%9F%B3%E4%B8%BA%E6%96%97%E7%BD%97%E5%A4%A7%E9%99%86%E8%B5%94%E5%81%BF8%E4%BA%BF%23&Refer=top', 'hotness': ' 442538'}, {'title': '磨牙是一种病', 'url': 'https://s.weibo.com/weibo?q=%23%E6%B2%B3%E5%8D%97%E4%B8%80%E9%A5%BA%E5%AD%90%E5%BA%97%E8%BF%9E%E7%BB%AD6%E5%B9%B4%E5%86%AC%E8%87%B3%E5%81%9C%E4%B8%9A%23&Refer=top', 'hotness': ' 431489'}, {'title': '夫妻吵架开车互撞', 'url': 'https://s.weibo.com/weibo?q=%23%E7%A3%A8%E7%89%99%E6%98%AF%E4%B8%80%E7%A7%8D%E7%97%85%23&Refer=top', 'hotness': ' 428648'}, {'title': '保利 如梦之梦', 'url': 'https://s.weibo.com/weibo?q=%23%E5%A4%AB%E5%A6%BB%E5%90%B5%E6%9E%B6%E5%BC%80%E8%BD%A6%E4%BA%92%E6%92%9E%23&Refer=top', 'hotness': ' 416940'}, {'title': '央视新闻评雄狮少年是文化自信', 'url': 'https://s.weibo.com/weibo?q=%E4%BF%9D%E5%88%A9%20%E5%A6%82%E6%A2%A6%E4%B9%8B%E6%A2%A6&Refer=top', 'hotness': ' 416067'}, {'title': '雄狮少年', 'url': 'https://s.weibo.com/weibo?q=%23%E5%A4%AE%E8%A7%86%E6%96%B0%E9%97%BB%E8%AF%84%E9%9B%84%E7%8B%AE%E5%B0%91%E5%B9%B4%E6%98%AF%E6%96%87%E5%8C%96%E8%87%AA%E4%BF%A1%23&Refer=top', 'hotness': ' 402796'}, {'title': '税务总局立案调查多领域偷逃税款问题', 'url': 'https://s.weibo.com/weibo?q=%E9%9B%84%E7%8B%AE%E5%B0%91%E5%B9%B4&Refer=top', 'hotness': ' 391470'}, {'title': '双向奔赴的爱有多温暖', 'url': 'https://s.weibo.com/weibo?q=%23%E7%A8%8E%E5%8A%A1%E6%80%BB%E5%B1%80%E7%AB%8B%E6%A1%88%E8%B0%83%E6%9F%A5%E5%A4%9A%E9%A2%86%E5%9F%9F%E5%81%B7%E9%80%83%E7%A8%8E%E6%AC%BE%E9%97%AE%E9%A2%98%23&Refer=top', 'hotness': ' 387672'}, {'title': '村民挖到旧坛子内藏30块鸦片', 'url': 'https://s.weibo.com/weibo?q=%23%E5%8F%8C%E5%90%91%E5%A5%94%E8%B5%B4%E7%9A%84%E7%88%B1%E6%9C%89%E5%A4%9A%E6%B8%A9%E6%9A%96%23&Refer=top', 'hotness': ' 382937'}, {'title': '冬至吃啥', 'url': 'https://s.weibo.com/weibo?q=%23%E6%9D%91%E6%B0%91%E6%8C%96%E5%88%B0%E6%97%A7%E5%9D%9B%E5%AD%90%E5%86%85%E8%97%8F30%E5%9D%97%E9%B8%A6%E7%89%87%23&Refer=top', 'hotness': ' 353842'}, {'title': '鹿晗空降超话', 'url': 'https://s.weibo.com/weibo?q=%23%E5%86%AC%E8%87%B3%E5%90%83%E5%95%A5%23&Refer=top', 'hotness': ' 353298'}, {'title': '雪滴花有哪些细思极恐的细节', 'url': 'https://s.weibo.com/weibo?q=%23%E9%B9%BF%E6%99%97%E7%A9%BA%E9%99%8D%E8%B6%85%E8%AF%9D%23&Refer=top', 'hotness': ' 352539'}, {'title': '金家骏还债钱被骗', 'url': 'https://s.weibo.com/weibo?q=%23%E9%9B%AA%E6%BB%B4%E8%8A%B1%E6%9C%89%E5%93%AA%E4%BA%9B%E7%BB%86%E6%80%9D%E6%9E%81%E6%81%90%E7%9A%84%E7%BB%86%E8%8A%82%23&Refer=top', 'hotness': ' 333359'}, {'title': '那年我们', 'url': 'https://s.weibo.com/weibo?q=%23%E9%87%91%E5%AE%B6%E9%AA%8F%E8%BF%98%E5%80%BA%E9%92%B1%E8%A2%AB%E9%AA%97%23&Refer=top', 'hotness': ' 318484'}, {'title': '陕西发布考研安排', 'url': 'https://s.weibo.com/weibo?q=%E9%82%A3%E5%B9%B4%E6%88%91%E4%BB%AC&Refer=top', 'hotness': ' 313620'}, {'title': '网购买过最丑的商品', 'url': 'https://s.weibo.com/weibo?q=%23%E9%99%95%E8%A5%BF%E5%8F%91%E5%B8%83%E8%80%83%E7%A0%94%E5%AE%89%E6%8E%92%23&Refer=top', 'hotness': ' 312586'}, {'title': '薇娅们赚那么多为什么还逃税', 'url': 'https://s.weibo.com/weibo?q=%23%E7%BD%91%E8%B4%AD%E4%B9%B0%E8%BF%87%E6%9C%80%E4%B8%91%E7%9A%84%E5%95%86%E5%93%81%23&Refer=top', 'hotness': ' 306259'}, {'title': '如何缓解考研前的焦虑情绪', 'url': 'https://s.weibo.com/weibo?q=%23%E8%96%87%E5%A8%85%E4%BB%AC%E8%B5%9A%E9%82%A3%E4%B9%88%E5%A4%9A%E4%B8%BA%E4%BB%80%E4%B9%88%E8%BF%98%E9%80%83%E7%A8%8E%23&Refer=top', 'hotness': ' 298989'}, {'title': '冬至月亮', 'url': 'https://s.weibo.com/weibo?q=%23%E5%A6%82%E4%BD%95%E7%BC%93%E8%A7%A3%E8%80%83%E7%A0%94%E5%89%8D%E7%9A%84%E7%84%A6%E8%99%91%E6%83%85%E7%BB%AA%23&Refer=top', 'hotness': ' 247795'}, {'title': '孙海洋驾车1700公里去山东接孙卓', 'url': 'https://s.weibo.com/weibo?q=%E5%86%AC%E8%87%B3%E6%9C%88%E4%BA%AE&Refer=top', 'hotness': ' 232799'}, {'title': '医学生还能用听诊器吃瓜', 'url': 'https://s.weibo.com/weibo?q=%23%E5%AD%99%E6%B5%B7%E6%B4%8B%E9%A9%BE%E8%BD%A61700%E5%85%AC%E9%87%8C%E5%8E%BB%E5%B1%B1%E4%B8%9C%E6%8E%A5%E5%AD%99%E5%8D%93%23&Refer=top', 'hotness': ' 232390'}, {'title': '王源分享冬至饺子图', 'url': 'https://s.weibo.com/weibo?q=%23%E5%8C%BB%E5%AD%A6%E7%94%9F%E8%BF%98%E8%83%BD%E7%94%A8%E5%90%AC%E8%AF%8A%E5%99%A8%E5%90%83%E7%93%9C%23&Refer=top', 'hotness': ' 231949'}, {'title': '石家庄乔家大厨7名顾客因食物中毒死亡系谣言', 'url': 'https://s.weibo.com/weibo?q=%23%E7%8E%8B%E6%BA%90%E5%88%86%E4%BA%AB%E5%86%AC%E8%87%B3%E9%A5%BA%E5%AD%90%E5%9B%BE%23&Refer=top', 'hotness': ' 231419'}, {'title': '2021婚礼社死大盘点', 'url': 'https://s.weibo.com/weibo?q=%23%E7%9F%B3%E5%AE%B6%E5%BA%84%E4%B9%94%E5%AE%B6%E5%A4%A7%E5%8E%A87%E5%90%8D%E9%A1%BE%E5%AE%A2%E5%9B%A0%E9%A3%9F%E7%89%A9%E4%B8%AD%E6%AF%92%E6%AD%BB%E4%BA%A1%E7%B3%BB%E8%B0%A3%E8%A8%80%23&Refer=top', 'hotness': ' 222487'}, {'title': '吃啥才配得上冬至的仪式感', 'url': 'https://s.weibo.com/weibo?q=%232021%E5%A9%9A%E7%A4%BC%E7%A4%BE%E6%AD%BB%E5%A4%A7%E7%9B%98%E7%82%B9%23&Refer=top', 'hotness': ' 216771'}, {'title': '西安疫情', 'url': 'https://s.weibo.com/weibo?q=%23%E5%90%83%E5%95%A5%E6%89%8D%E9%85%8D%E5%BE%97%E4%B8%8A%E5%86%AC%E8%87%B3%E7%9A%84%E4%BB%AA%E5%BC%8F%E6%84%9F%23&Refer=top', 'hotness': ' 213049'}, {'title': '北半球最长的夜晚', 'url': 'https://s.weibo.com/weibo?q=%23%E8%A5%BF%E5%AE%89%E7%96%AB%E6%83%85%23&Refer=top', 'hotness': ' 190558'}, {'title': '西安流调', 'url': 'https://s.weibo.com/weibo?q=%E5%8C%97%E5%8D%8A%E7%90%83%E6%9C%80%E9%95%BF%E7%9A%84%E5%A4%9C%E6%99%9A&Refer=top', 'hotness': ' 185172'}, {'title': '让耳背的奶奶开门有多难', 'url': 'https://s.weibo.com/weibo?q=%23%E8%A5%BF%E5%AE%89%E6%B5%81%E8%B0%83%23&Refer=top', 'hotness': ' 185142'}, {'title': '被陌生人治愈到的瞬间', 'url': 'https://s.weibo.com/weibo?q=%23%E8%AE%A9%E8%80%B3%E8%83%8C%E7%9A%84%E5%A5%B6%E5%A5%B6%E5%BC%80%E9%97%A8%E6%9C%89%E5%A4%9A%E9%9A%BE%23&Refer=top', 'hotness': ' 158734'}]

我们将其存入txt中待用

代码总览

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from bs4 import BeautifulSoup
import requests
import time
news = []
driver = webdriver.Chrome()
print('准备登陆Weibo.cn网站...')
driver.get("https://login.sina.com.cn/signup/signin.php")
wait = WebDriverWait(driver, 5)
#暂停1分钟进行预登陆,填写账号密码及验证
time.sleep(60)
cookies = driver.get_cookies()
s = requests.Session()
c = requests.cookies.RequestsCookieJar()
for item in cookies:c.set(item["name"],item["value"])
print(c)
s.cookies.update(c) # 载入cookie
r = s.get('https://s.weibo.com/top/summary')
r.encoding = 'utf-8'
soup = BeautifulSoup(r.text, 'lxml')urls_titles = soup.select('#pl_top_realtimehot > table > tbody > tr > td.td-02 > a')
hotness = soup.select('#pl_top_realtimehot > table > tbody > tr > td.td-02 > span')for i in range(len(urls_titles)-1):hot_news = {}hot_news['title'] = urls_titles[i+1].get_text()hot_news['url'] = "https://s.weibo.com"+urls_titles[i]['href']hot_news['hotness'] = hotness[i].get_text()news.append(hot_news)print(news)

2.词云

1.数据处理

jieba是python的一个中文分词库,

words = jieba.cut(hots)

我们利用jiaba模块对热搜词汇进行拆分。

2.生成词云

cloud = WordCloud(font_path='C:\Users\Debutante\宋体.ttf',  # 字体路径background_color='white',  # 背景颜色width=1920,height=1080,max_font_size=50,  # 字体大小min_font_size=8,max_words=1000,#最多次数#mask=np.array(PIL.Image.open('C:\Users\Debutante\Pictures\1.jpg')可以根据图案生成词云图
)
cloud.generate(word)

2.展示图片

plt.figure('tuzi')  # 图片显示的名字
plt.imshow(cloud)
plt.axis('off')  # 关闭坐标
plt.show()

爬取微博热搜制作词云相关推荐

  1. python 爬关键词,Python爬虫实战:爬取微博热搜关键词

    一.爬取微博热搜关键词需要的第三方库 1.requests 2.BeautifulSoup 美味汤 3.worldcloud 词云 4.jieba 中文分词 5.matplotlib 绘图 二.爬取微 ...

  2. python不间断爬取微博热搜并存储

    Q:想要记录实时的微博热搜分析某一类事件的热度怎么办? A: 采用python爬虫24小时不间断爬取微博热搜并存储到本地. 代码如下: # -*- coding: utf-8 -*- "&q ...

  3. 爬取微博热搜并发送提醒(邮件和server酱)

    爬取微博热搜并发送提醒 写在前面 用到的包 全局变量 爬取数据 发送邮件 开始任务 发起推送 入口函数 定时提醒 注意事项 结果展示 请参考以下内容 写在前面 今天在CSDN看到了一篇爬取微博热搜并发 ...

  4. Python爬取微博热搜数据之炫酷可视化

    可视化展示 看完记得点个赞哟 微博炫酷可视化音乐组合版来了! 项目介绍 背景 现阶段,微博.抖音.快手.哗哩哗哩.微信公众号已经成为不少年轻人必备的"生活神器".在21世纪的今天, ...

  5. python 爬虫热搜_Python网络爬虫之爬取微博热搜

    微博热搜的爬取较为简单,我只是用了lxml和requests两个库 1.分析网页的源代码:右键--查看网页源代码. 从网页代码中可以获取到信息 (1)热搜的名字都在 的子节点里 (2)热搜的排名都在 ...

  6. python爬取微博热搜并存入表格_python爬虫进阶之爬取微博热搜存入Mysql

    在编程中,我们如果想要把数据转入数据库中,首先会选择 MySQL数据库.因为MySQL数据库体积小.速度快.总体拥有成本低.开放源代码,其有着广泛的应用,例如我们使用python爬虫微博热搜,就可以使 ...

  7. 爬取新笔趣阁排行并保存到mysql_python+selenium爬取微博热搜存入Mysql的实现方法...

    最终的效果 废话不多少,直接上图 这里可以清楚的看到,数据库里包含了日期,内容,和网站link 下面我们来分析怎么实现 使用的库 import requests from selenium.webdr ...

  8. php获取微博热搜,爬取微博热搜top50(示例代码)

    一.主题式网络爬虫设计方案(15分) 1.主题式网络爬虫名称:爬取微博热搜top50 2.主题式网络爬虫的内容与数据特征分析:排名 关键词 点击量 3.主题式网络爬虫设计方案概述: 先分析页面 对比源 ...

  9. 从零到一学爬虫-爬取微博热搜示例

    爬取微博热搜榜-简单示例 使用爬虫模拟浏览器向微博热搜的服务器发送请求,得到响应,然后将响应的信息进行打印. 当我们直接打开浏览器,输入https://s.weibo.com/top/summary, ...

最新文章

  1. 树状数组模板1——单点修改区间查询
  2. rust种的南瓜为什么老是消失_冷天早餐别将就,用南瓜和萝卜这样做,鲜香松软,营养好吃又管饱...
  3. 手机突然电量消耗很快_手机掉电突然变快?这5点操作你肯定至少做了其中一个!...
  4. 软件质量保证计划_质量保证QA与质量控制QC
  5. python数字识别关键技术_用Python从零开始设计数字图片识别神经网络--搭建基本架构...
  6. OpenVINO安装步骤
  7. Spire PDF转WORD (高效不失真)
  8. 《山海经》异兽75种,附图
  9. unity Google 广告接入 SDK Android
  10. 机器学习--PCA(主成分分析)
  11. P2P网络与BitTorrent技术简介
  12. 基于JavaWeb实现的问卷调查系统
  13. 猿圈 题库_小猿圈总结常见HTML5基础知识面试题汇总
  14. 经典四大排序(动图实现)
  15. java swing 跳转窗口_java swing 怎么实现点击按钮或者某个组件,跳转到另一个页面或者窗体?...
  16. 植物神经紊乱的影响,你知道有哪些吗?
  17. 极大似然估计四个步骤
  18. php codis,codis集群部署实战
  19. vba 抓取 统计用区划和城乡划分代码 到 电子表格
  20. UIM卡 PIN 码特点

热门文章

  1. WuThreat身份安全云-TVD每日漏洞情报-2022-12-27
  2. TFTP文件传输协议
  3. 目标检测中的Anchor-free回顾
  4. 使用组策略将用户文件夹重定向到D盘
  5. SkeyeVSS智慧社区综合安防视频管理系统方案 助力小区安防建设智能化
  6. 删除设备和驱动器中的迅雷下载、百度网盘的图标(win10、win11可用)
  7. Dubbo:Dubbo服务发现
  8. mysql5.6 之frm,MYD,MYI,idb,文件说明
  9. winhex快捷键使用
  10. 弯下腰,只为一个昂起头的机会