濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > 如何用python爬取微博熱搜數(shù)據(jù)并保存

如何用python爬取微博熱搜數(shù)據(jù)并保存

熱門(mén)標(biāo)簽:高德地圖標(biāo)注字母 騰訊地圖標(biāo)注有什么版本 400電話(huà)辦理費(fèi)用收費(fèi) 千呼ai電話(huà)機(jī)器人免費(fèi) 鎮(zhèn)江人工外呼系統(tǒng)供應(yīng)商 深圳網(wǎng)絡(luò)外呼系統(tǒng)代理商 申請(qǐng)辦個(gè)400電話(huà)號(hào)碼 外呼系統(tǒng)前面有錄音播放嗎 柳州正規(guī)電銷(xiāo)機(jī)器人收費(fèi)

主要用到requests和bf4兩個(gè)庫(kù)
將獲得的信息保存在d://hotsearch.txt下

import requests;
import bs4
mylist=[]
r = requests.get(url='https://s.weibo.com/top/summary?Refer=top_hottopnav=1wvr=6',timeout=10)
print(r.status_code) # 獲取返回狀態(tài)
r.encoding=r.apparent_encoding
demo = r.text
from bs4 import BeautifulSoup
soup = BeautifulSoup(demo,"html.parser")
for link in soup.find('tbody') :
 hotnumber=''
 if isinstance(link,bs4.element.Tag):
#  print(link('td'))
  lis=link('td')
  hotrank=lis[1]('a')[0].string#熱搜排名
  hotname=lis[1].find('span')#熱搜名稱(chēng)
  if isinstance(hotname,bs4.element.Tag):
   hotnumber=hotname.string#熱搜指數(shù)
   pass
  mylist.append([lis[0].string,hotrank,hotnumber,lis[2].string])
f=open("d://hotsearch.txt","w+")
for line in mylist:
 f.write('%s %s %s %s\n'%(line[0],line[1],line[2],line[3]))

知識(shí)點(diǎn)擴(kuò)展:利用python爬取微博熱搜并進(jìn)行數(shù)據(jù)分析

爬取微博熱搜

import schedule
import pandas as pd
from datetime import datetime
import requests
from bs4 import BeautifulSoup

url = "https://s.weibo.com/top/summary?cate=realtimehotsudaref=s.weibo.comdisplay=0retcode=6102"
get_info_dict = {}
count = 0

def main():
  global url, get_info_dict, count
  get_info_list = []
  print("正在爬取數(shù)據(jù)~~~")
  html = requests.get(url).text
  soup = BeautifulSoup(html, 'lxml')
  for tr in soup.find_all(name='tr', class_=''):
    get_info = get_info_dict.copy()
    get_info['title'] = tr.find(class_='td-02').find(name='a').text
    try:
      get_info['num'] = eval(tr.find(class_='td-02').find(name='span').text)
    except AttributeError:
      get_info['num'] = None
    get_info['time'] = datetime.now().strftime("%Y/%m/%d %H:%M")
    get_info_list.append(get_info)
  get_info_list = get_info_list[1:16]
  df = pd.DataFrame(get_info_list)
  if count == 0:
    df.to_csv('datas.csv', mode='a+', index=False, encoding='gbk')
    count += 1
  else:
    df.to_csv('datas.csv', mode='a+', index=False, header=False, encoding='gbk')

# 定時(shí)爬蟲(chóng)
schedule.every(1).minutes.do(main)

while True:
  schedule.run_pending()

pyecharts數(shù)據(jù)分析

import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import Bar, Timeline, Grid
from pyecharts.globals import ThemeType, CurrentConfig

df = pd.read_csv('datas.csv', encoding='gbk')
print(df)
t = Timeline(init_opts=opts.InitOpts(theme=ThemeType.MACARONS)) # 定制主題
for i in range(int(df.shape[0]/15)):
  bar = (
    Bar()
      .add_xaxis(list(df['title'][i*15: i*15+15][::-1])) # x軸數(shù)據(jù)
      .add_yaxis('num', list(df['num'][i*15: i*15+15][::-1])) # y軸數(shù)據(jù)
      .reversal_axis() # 翻轉(zhuǎn)
      .set_global_opts( # 全局配置項(xiàng)
      title_opts=opts.TitleOpts( # 標(biāo)題配置項(xiàng)
        title=f"{list(df['time'])[i * 15]}",
        pos_right="5%", pos_bottom="15%",
        title_textstyle_opts=opts.TextStyleOpts(
          font_family='KaiTi', font_size=24, color='#FF1493'
        )
      ),
      xaxis_opts=opts.AxisOpts( # x軸配置項(xiàng)
        splitline_opts=opts.SplitLineOpts(is_show=True),
      ),
      yaxis_opts=opts.AxisOpts( # y軸配置項(xiàng)
        splitline_opts=opts.SplitLineOpts(is_show=True),
        axislabel_opts=opts.LabelOpts(color='#DC143C')
      )
    )
      .set_series_opts( # 系列配置項(xiàng)
      label_opts=opts.LabelOpts( # 標(biāo)簽配置
        position="right", color='#9400D3')
    )
  )
  grid = (
    Grid()
      .add(bar, grid_opts=opts.GridOpts(pos_left="24%"))
  )
  t.add(grid, "")
  t.add_schema(
    play_interval=1000, # 輪播速度
    is_timeline_show=False, # 是否顯示 timeline 組件
    is_auto_play=True, # 是否自動(dòng)播放
  )

t.render('時(shí)間輪播圖.html')

到此這篇關(guān)于如何用python爬取微博熱搜數(shù)據(jù)并保存的文章就介紹到這了,更多相關(guān)python爬取微博熱搜數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • python爬取天氣數(shù)據(jù)的實(shí)例詳解
  • 用python爬取歷史天氣數(shù)據(jù)的方法示例
  • python爬取哈爾濱天氣信息
  • python3爬取各類(lèi)天氣信息
  • Python爬取國(guó)外天氣預(yù)報(bào)網(wǎng)站的方法
  • Python爬蟲(chóng)爬取微博熱搜保存為 Markdown 文件的源碼
  • python+selenium爬取微博熱搜存入Mysql的實(shí)現(xiàn)方法
  • Python網(wǎng)絡(luò)爬蟲(chóng)之爬取微博熱搜
  • python趣味挑戰(zhàn)之爬取天氣與微博熱搜并自動(dòng)發(fā)給微信好友

標(biāo)簽:烏蘭察布 平頂山 合肥 大慶 郴州 烏蘭察布 哈爾濱 海南

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《如何用python爬取微博熱搜數(shù)據(jù)并保存》,本文關(guān)鍵詞  如,何用,python,爬取,微博,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《如何用python爬取微博熱搜數(shù)據(jù)并保存》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于如何用python爬取微博熱搜數(shù)據(jù)并保存的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    普宁市| 观塘区| 漳浦县| 高安市| 揭西县| 象州县| 广河县| 古浪县| 昭苏县| 安仁县| 夏邑县| 郎溪县| 垦利县| 中牟县| 台中市| 万州区| 弥勒县| 襄樊市| 鹤岗市| 从化市| 皋兰县| 惠来县| 八宿县| 淅川县| 车致| 镶黄旗| 瑞安市| 惠来县| 万山特区| 大丰市| 岱山县| 溧水县| 广宁县| 新密市| 梨树县| 若尔盖县| 久治县| 宁津县| 林芝县| 故城县| 揭西县|