Python爬蟲爬取微博熱搜保存為 Markdown 文件的源碼
什么是爬蟲?
網(wǎng)絡(luò)爬蟲(又被稱為網(wǎng)頁蜘蛛,網(wǎng)絡(luò)機器人,在FOAF社區(qū)中間,更經(jīng)常的稱為網(wǎng)頁追逐者),是一種按照一定的規(guī)則,自動地抓取萬維網(wǎng)信息的程序或者腳本。另外一些不常使用的名字還有螞蟻、自動索引、模擬程序或者蠕蟲。
其實通俗的講就是通過程序去獲取web頁面上自己想要的數(shù)據(jù),也就是自動抓取數(shù)據(jù)
爬蟲可以做什么?
你可以爬取小姐姐的圖片,爬取自己有興趣的島國視頻,或者其他任何你想要的東西,前提是,你想要的資源必須可以通過瀏覽器訪問的到。
爬蟲的本質(zhì)是什么?
上面關(guān)于爬蟲可以做什么,定義了一個前提,是瀏覽器可以訪問到的任何資源,特別是對于知曉web請求生命周期的學者來說,爬蟲的本質(zhì)就更簡單了。爬蟲的本質(zhì)就是模擬瀏覽器打開網(wǎng)頁,獲取網(wǎng)頁中我們想要的那部分數(shù)據(jù)。
微博熱搜榜python爬蟲,僅供學習交流

源碼及注釋:
# -*- coding=UTF-8 -*-
#!usr/bin/env python
import os
import time
import requests
from lxml import etree
url = "https://s.weibo.com/top/summary?cate=realtimehot"
headers={
'Host': 's.weibo.com',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Connection': 'keep-alive',
'Referer': 'https://weibo.com/',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36'
}
r = requests.get(url,headers=headers)
print(r.status_code)
html_xpath = etree.HTML(r.text)
data = html_xpath.xpath('//*[@id="pl_top_realtimehot"]/table/tbody/tr/td[2]')
num = -1
# # 解決存儲路徑
# time_path = time.strftime('%Y{y}%m{m}%dublnpf9mb',time.localtime()).format(y='年', m='月', d='日')
# time_name = time.strftime('%Y{y}%m{m}%dublnpf9mb%H{h}',time.localtime()).format(y='年', m='月', d='日',h='點')
# root = "./" + time_path + "/"
# path = root + time_name + '.md'
# if not os.path.exists(root):
# os.mkdir(root)
# 解決存儲路徑
time_path = time.strftime('%Y{y}%m{m}%dublnpf9mb',time.localtime()).format(y='年', m='月', d='日')
time_name = time.strftime('%Y{y}%m{m}%dublnpf9mb%H{h}',time.localtime()).format(y='年', m='月', d='日',h='點')
year_path = time.strftime('%Y{y}',time.localtime()).format(y='年')
month_path = time.strftime('%m{m}',time.localtime()).format(m='月')
day_month = time.strftime('%dublnpf9mb',time.localtime()).format(d='日')
all_path = "./" + year_path + '/'+ month_path + '/' + day_month
if not os.path.exists(all_path):
# 創(chuàng)建多層路徑
os.makedirs(all_path)
# 最終文件存儲位置
root = all_path + "/"
path = root + time_name + '.md'
print(path)
# 文件頭部信息
with open(path,'a') as f:
f.write('{} {}\n\n'.format('# ',time_name+'數(shù)據(jù)'))
f.close()
for tr in (data):
title = tr.xpath('./a/text()')
hot_score = tr.xpath('./span/text()')
num += 1
# 過濾第 0 條
if num == 0:
pass
else:
with open(path,'a') as f:
f.write('{} {}、{}\n\n'.format('###',num,title[0]))
f.write('{} {}\n\n'.format('微博當時熱度為:',hot_score[0]))
f.close()
print(num,title[0],'微博此時的熱度為:',hot_score[0])
運行:
運行結(jié)束后會在當前文件夾下生成以時間命名的文件夾,并且會生成以具體小時為單位的具體時間命名的 Markdown 文件,如下:

查看:

到此這篇關(guān)于Python爬蟲爬取微博熱搜保存為 Markdown 文件的文章就介紹到這了,更多相關(guān)Python爬蟲爬取微博熱搜保存內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
利用Python批量導出mysql數(shù)據(jù)庫表結(jié)構(gòu)的操作實例
這篇文章主要給大家介紹了關(guān)于利用Python批量導出mysql數(shù)據(jù)庫表結(jié)構(gòu)的相關(guān)資料,需要的朋友可以參考下2022-08-08
關(guān)于adfuller函數(shù)返回值的參數(shù)說明與記錄
這篇文章主要介紹了關(guān)于adfuller函數(shù)返回值的參數(shù)說明與記錄,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11
詳解一種用django_cache實現(xiàn)分布式鎖的方式
這篇文章主要介紹了詳解一種用django_cache實現(xiàn)分布式鎖的方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-09-09
基于Django OneToOneField和ForeignKey的區(qū)別詳解
這篇文章主要介紹了基于Django OneToOneField和ForeignKey的區(qū)別詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Python socket網(wǎng)絡(luò)編程TCP/IP服務(wù)器與客戶端通信
這篇文章主要介紹了Python socket網(wǎng)絡(luò)編程TCP/IP服務(wù)器與客戶端通信的相關(guān)資料,這里對Scoket 進行詳解并創(chuàng)建TCP服務(wù)器及TCP 客戶端實例代碼,需要的朋友可以參考下2017-01-01

