項(xiàng)目地址:
https://github.com/cgDeepLearn/BilibiliCrawler
項(xiàng)目特點(diǎn)
- 采取了一定的反反爬策略。
- Bilibili更改了用戶頁面的api, 用戶抓取解析程序需要重構(gòu)。
快速開始
- 拉取項(xiàng)目, git clone https://github.com/cgDeepLearn/BilibiliCrawler.git
- 進(jìn)入項(xiàng)目主目錄,安裝虛擬環(huán)境crawlenv(請參考使用說明里的虛擬環(huán)境安裝)。
- 激活環(huán)境并在主目錄運(yùn)行crawl,爬取結(jié)果將保存在data目錄csv文件中。
ource activate crawlenv
python initial.py file # 初始化file模式
python crawl_user.py file 1 100 # file模式,1 100是開始、結(jié)束bilibili的uid
進(jìn)入data目錄查看抓取的數(shù)據(jù),是不是很簡單!
如果需要使用數(shù)據(jù)庫保存和一些其他的設(shè)置,請看下面的使用說明
使用說明
1.拉取項(xiàng)目
git clone https://github.com/cgDeepLearn/BilibiliCrawler.git
2.進(jìn)入項(xiàng)目主目錄, 安裝虛擬環(huán)境
conda create -n crawlenv python=3.6
source activate crawlenv # 激活虛擬環(huán)境
pip install -r requirements.txt
virtualenv crawlenv
source crawlenv/bin/activate # 激活虛擬環(huán)境,windows下不用source
pip install -r requirements.txt # 安裝項(xiàng)目依賴
3. 修改配置文件
進(jìn)入config目錄,修改config.ini配置文件(默認(rèn)使用的是postgresql數(shù)據(jù)庫,如果你是使用的是postgresql,只需要將其中的參數(shù)替換成你的,下面其他的步驟可以忽略) 數(shù)據(jù)庫配置選擇其中一個你本地安裝的即可,將參數(shù)更換成你的 如果你需要更自動化的數(shù)據(jù)庫配置,請移步我的DB_ORM項(xiàng)目
[db_mysql]
user = test
password = test
host = localhost
port = 3306
dbname = testdb
[db_postgresql]
user = test
password = test
host = localhost
port = 5432
dbname = testdb
然后修改conf.py中獲取配置文件的函數(shù)
def get_db_args():
"""
獲取數(shù)據(jù)庫配置信息
"""
return dict(CONFIG.items('db_postgresql')) # 如果安裝的是mysql,請將參數(shù)替換為db_mysql
進(jìn)入db目錄,修改basic.py的連接數(shù)據(jù)庫的DSN
# connect_str = "postgresql+psycopg2://{}:{}@{}:{}/{}".format(kwargs['user'], kwargs['password'], kwargs['host'], kwargs['port'], kwargs['dbname'])
# 若使用的是mysql,請將上面的connect_str替換成下面的
connect_str = "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(kwargs['user'], kwargs['password'], kwargs['host'], kwargs['port'], kwargs['dbname'])
# sqlite3,mongo等請移步我的DB_ORM項(xiàng)目,其他一些數(shù)據(jù)庫也將添加支持
4. 運(yùn)行爬蟲
- 在主目錄激活虛擬環(huán)境, 初次運(yùn)行請執(zhí)行
python initial.py db # db模式,file模式請將db換成file
# file模式會將抓取結(jié)果保存在data目錄
# db模式會將數(shù)據(jù)保存在設(shè)置好的數(shù)據(jù)庫中
# 若再次以db模式運(yùn)行將會drop所有表后再create,初次運(yùn)行后請慎重再次使用!!!
# 如果修改添加了表,并不想清空數(shù)據(jù),請運(yùn)行 python create_all.py
python crawl_user.py db 1 10000 # crawl_user 抓取用戶數(shù)據(jù),db 保存在數(shù)據(jù)庫中, 1 10000為抓取起止id
python crawl_video_ajax.py db 1 100 # crawl_video_ajax 抓取視頻ajax信息保存到數(shù)據(jù)庫中,
python crawl_user_video.py db 1 10000 #同時抓取user 和videoinfo
# 示例為uid從1到100的user如果有投稿視頻則抓取其投稿視頻的信息,
# 若想通過視頻id逐個抓取請運(yùn)行python crawl_video_by_aid.py db 1 1000
程序內(nèi)已進(jìn)行了一些抓取速率的設(shè)置,但各機(jī)器cpu、mem不同抓取速率也不同,請酌情修改
太快太慢請修改各crawl中的sleepsec參數(shù),ip會被限制訪問頻率,overspeed會導(dǎo)致爬取數(shù)據(jù)不全,
之后會添加運(yùn)行參數(shù)speed(high, low),不用再手動配置速率
爬取日志在logs目錄
user, video分別為用戶和視頻的爬取日志
storage為數(shù)據(jù)庫日志 如需更換log格式,請修改logger模塊
linux下運(yùn)行python ......前面加上nohup,例如:
nohup python crawl_user db 1 10000
程序輸出保存文件,默認(rèn)會包存在主目錄額nohup.out文件中,添加 > fielname就會保存在設(shè)置的文件中:
nohup python crawl_video_ajax.py db 1 1000 > video_ajaxup_1_1000.out # 輸出將保存在video_ajaxup_1_1000.out中
程序多線程使用的生產(chǎn)者消費(fèi)者模式中產(chǎn)生了程序運(yùn)行的狀況的打印信息,類似如下
produce 1_1
consumed 1_1
...
如想運(yùn)行更快,請在程序各項(xiàng)設(shè)置好后注釋掉其中的打印程序
# utils/pcModels.py
print('[+] produce %s_%s' % (index, pitem)) # 請注釋掉
print('[-] consumed %s_%s\n' % (index, data)) # 請注釋掉
更多
項(xiàng)目是單機(jī)多線程,若想使用分布式爬取,請參考Crawler-Celery
以上就是python 爬取嗶哩嗶哩up主信息和投稿視頻的詳細(xì)內(nèi)容,更多關(guān)于python 爬取嗶哩嗶哩的資料請關(guān)注腳本之家其它相關(guān)文章!
您可能感興趣的文章:- python b站視頻下載的五種版本
- 基于python對B站收藏夾按照視頻發(fā)布時間進(jìn)行排序的問題
- 教你如何使用Python下載B站視頻的詳細(xì)教程
- 利用python+ffmpeg合并B站視頻及格式轉(zhuǎn)換的實(shí)例代碼
- ffmpeg+Python實(shí)現(xiàn)B站MP4格式音頻與視頻的合并示例代碼
- python 爬取B站原視頻的實(shí)例代碼
- Python如何爬取b站熱門視頻并導(dǎo)入Excel
- Python如何實(shí)現(xiàn)爬取B站視頻
- python3寫爬取B站視頻彈幕功能