與python爬蟲的切磋——爬取bilibili某用戶的粉絲數

by rocky

那個男孩不想擁有自己的爬蟲~

在我歷盡千辛萬苦尋找資料后,終於匯總出了這篇文章【笑cry】

Let's dance!

首先,我們要加載相應的第三方庫:

requests

我用的編譯器是pycharm

所以加載第三方庫就比較簡單了

【絕對不是因爲我不會在vscode裏面導入】

源碼是在av84633631的基礎上做修改而成。

 

import requests
import time
import os
import re

def dailichi():
    import random
    daili = [
        'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36',
        'Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; WOW64; Trident/4.0; SLCC1)',
        'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; WOW64; Trident/4.0; SLCC1)',
        'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1',
        'Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1',
        'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.71',
        'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; QQBrowser/7.0.3698.400)',
        'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E)',
        'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11'
    ]
    dai = random.choice(daili)
    # print(dai)
    head = {
        'User-Agent': '%s' % dai
    }
    return head


def get_time():
    import datetime
    # now_time = str(datetime.datetime.now())[:10]
    now_time = str(datetime.datetime.now())
    return now_time


def main(id):
    url = f'https://api.bilibili.com/x/relation/stat?vmid={id}'
    timee = get_time()
    response = requests.get(url=url, headers=dailichi())
    pep = response.json()['data']['follower']
    return str(timee) + ":" + str(pep)


def get_name(id):
    url = f'https://space.bilibili.com/{id}'
    response = requests.get(url=url, headers=dailichi())
    txt = response.text
    name = re.findall('(.*?)的个人空间 - 哔哩哔哩', txt, re.S)[0]
    return name


if __name__ == "__main__":
    print('輸入你的用戶UID')
    id = input()
    name = get_name(id)
    # print(name)
    while True:
        # os.system('cls')
        print('\r' + str(name) + '----' + main(id))
        time.sleep(1)

b = tk.Button(window, text='確定', font=('Arial', 12), width=10, height=1, command=hit_me)
b.pack()
</span></pre>

<p> </p>

<p><span style="font-family:Arial,Helvetica,sans-serif;">效果如下</span></p>

<p><span style="font-family:Arial,Helvetica,sans-serif;"> 【圖放不上,文字演示】</span></p>

<p><span style="font-family:Arial,Helvetica,sans-serif;">輸入你的用戶UID  【運行前,獲取所需用戶的uid】<br />
390686701       【鍵入uid(快去關注)】<br />
<span style="color:#2ecc71;">番茄酱原创空间</span>----<span style="color:#3498db;">2020-02-12 22:50:19.493535</span>:<span style="color:#e74c3c;">33</span> 【輸出結果】<br />
番茄酱原创空间----2020-02-12 22:50:20.816755:33  <span style="color:#3498db;">藍色是時間</span><br />
番茄酱原创空间----2020-02-12 22:50:22.049932:33  <span style="color:#e74c3c;">紅色是粉絲數</span><br />
番茄酱原创空间----2020-02-12 22:50:23.295451:33  <span style="color:#2ecc71;">綠色是用戶名</span><br />
番茄酱原创空间----2020-02-12 22:50:24.570366:33  每隔約一秒輸出一個結果<br />
番茄酱原创空间----2020-02-12 22:50:25.785704:33<br />
番茄酱原创空间----2020-02-12 22:50:26.973337:33<br />
番茄酱原创空间----2020-02-12 22:50:28.215945:33<br />
番茄酱原创空间----2020-02-12 22:50:29.389261:33<br />
番茄酱原创空间----2020-02-12 22:50:30.582280:33<br />
番茄酱原创空间----2020-02-12 22:50:31.756264:33</span></p>

<p><span style="font-family:Arial,Helvetica,sans-serif;">還可以知道別的一些大up豬的粉絲</span></p>

<p><span style="font-family:Arial,Helvetica,sans-serif;">THE END</span></p>

<p><span style="font-family:Arial,Helvetica,sans-serif;">點贊+評論=創作動力!!!!!</span></p>

                                
                                                            </div>
                             <div style="text-align: center;">
                                                                                                                
                                  <div class="WB_face fend_app_reward_persons">
                                   <ul class="fend_app_reward_persons_real">
                                                                    <div style="clear: both;"></div>
                                   </ul>
                                  </div>
                           </div>
                                                       <div class="meta clearfix">
                                <div class="aw-article-vote pull-left disabled">
                                    <a href="javascript:;" class="agree" onclick="AWS.User.article_vote($(this), 99, 1);"><i class="icon icon-agree"></i> <b>0</b></a>
                                                                    </div>

                                <span class="pull-right  more-operate">
                                    
                                    
                                    <a class="text-color-999 dropdown-toggle" data-toggle="dropdown" id="article_share">
                                        <i class="icon icon-share"></i> 分享                                    </a>
                                    <div class="aw-dropdown shareout pull-right bdsharebuttonbox">
                                        <ul class="aw-dropdown-list">
                                            <li><a href="#" class="bds_weixin" data-cmd="weixin" title="分享到微信"> 微信</a></li>
                                            <li><a href="#" class="bds_sqq" data-cmd="sqq" title="分享到QQ"> QQ</a></li>
                                            <li><a href="#" class="bds_tsina" data-cmd="tsina" title="分享到新浪微博"> 微博</a></li>
                                            <li><a href="#" class="bds_qzone" data-cmd="qzone" title="分享到QQ空间"> 空间</a></li>
                                        </ul>
                                    </div>
                                                                        <em class="text-color-999">2020-02-12</em>
                                </span>
                            </div>
                           
                        </div>
                        <div class="mod-footer">
                            
                        </div>
                                             </div>

                    <!-- 文章评论 -->
                    <div class="aw-mod">
                        <div class="mod-head common-head">
                            <h2>0 个评论</h2>
                        </div>

                        <div class="mod-body aw-feed-list">
                                                    </div>

                        
                                            </div>
                    <!-- end 文章评论 -->

                    <!-- 回复编辑器 -->
                    <div class="aw-mod aw-article-replay-box">
                        <a name="answer_form"></a>
                                                <p align="center">要回复文章请先<a href="http://maker.xlgy.net/?/account/login/">登录</a>或<a href="http://maker.xlgy.net/?/account/register/">注册</a></p>
                                            </div>
                    <!-- end 回复编辑器 -->
                </div>
                <!-- 侧边栏 -->
                <div class="col-sm-12 col-md-3 aw-side-bar hidden-sm hidden-xs">
                    <!-- 发起人 -->
                                        <div class="aw-mod user-detail">
                        <div class="mod-head">
                            <h3>发起人</h3>
                        </div>
                        <div class="mod-body">
                            <dl>
                                <dt class="pull-left aw-border-radius-5">
                                    <a href="http://maker.xlgy.net/?/people/6"><img alt="Rokey" src="http://maker.xlgy.net/uploads/avatar/000/00/00/06_avatar_mid.jpg" /></a>
                                </dt>
                                <dd class="pull-left">
                                    <a class="aw-user-name" href="http://maker.xlgy.net/?/people/6" data-id="6">Rokey</a>
                                                                            <i class="icon-v" title="个人认证"></i>
                                    
                                                                        <p>00后创客爱好者,颜值高,IQ高,爱交朋友。</p>
                                </dd>
                            </dl>
                        </div>
                        <div class="mod-footer clearfix">
                                                    </div>
                    </div>
                                        <!-- end 发起人 -->

                    
                                    </div>
                <!-- end 侧边栏 -->
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
    var ANSWER_EDIT_TIME = 30;
var pay_data={
            'url':G_BASE_URL+'/explore/doact/?p=reward&a=add',
            'remarks':"打赏Rokey",
            'both_id':"99",
            'passive_uid':"6",
            'cate':2,
            'notify_url':G_BASE_URL+'/pay/notify/'
        };
    $(document).ready(function () {
        if ($('.aw-article-vote.disabled').length)
        {
            $('.aw-article-vote.disabled a').attr('onclick', '');
        }

        $('.operate .btn-group').click(function(){
            if ($(this).hasClass('open'))
            {
                $(this).removeClass('open');
            }
            else
            {
                $(this).addClass('open');
            }
        });

        AWS.at_user_lists('#wmd-input');

        AWS.Init.init_article_comment_box($('.aw-article-comment'));
        $("#article_share").click(function(){
            $('#share_content').val('與python爬蟲的切磋——爬取bilibili某用戶的粉絲數');
            $('#share_title').val('與python爬蟲的切磋——爬取bilibili某用戶的粉絲數');
            $('#share_url').val(G_BASE_URL + '/article/99');

        });
        window._bd_share_config={
            "common":{
                bdText : '與python爬蟲的切磋——爬取bilibili某用戶的粉絲數',
                bdDesc : '與python爬蟲的切磋——爬取bilibili某用戶的粉絲數',
                bdUrl : G_BASE_URL + '/question/99',
                onBeforeClick:function(cmd,config){
                    return {bdText:$('#share_content').val(),
                        bdDesc:$('#share_title').val(),
                        bdUrl:$('#share_url').val()
                    }

                }
            },
            "share" : [{
                bdCustomStyle :G_STATIC_URL +"/api/css/share.css",
            }]
        };
        // window._bd_share_main={
        //     "jscfg":{
        //         domain:{
        //         staticUrl:"http://maker.xlgy.net"
        //         }
        //     },
        // }
        with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src=G_STATIC_URL +'/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];
    });
    </script>
<div class="aw-footer-wrap">
    <div class="aw-footer">
        Copyright © 2020<span
                class="hidden-xs"> -  粤ICP备17008331号-1, All Rights Reserved</span>

        <span class="hidden-xs">Powered By <a href="http://www.wecenter.com/?copyright"
                                              target="blank">WeCenter 3.3.4</a></span>

            </div>
</div>

<a class="aw-back-top hidden-xs" href="javascript:;" onclick="$.scrollTo(1, 600, {queue:true});"><i
            class="icon icon-up"></i></a>


<!-- DO NOT REMOVE -->
<div id="aw-ajax-box" class="aw-ajax-box"></div>

<div style="display:none;" id="__crond">
    <script type="text/javascript">
        $(document).ready(function () {
            $('#__crond').html(unescape('%3Cimg%20src%3D%22' + G_BASE_URL + '/crond/run/1585903525%22%20width%3D%221%22%20height%3D%221%22%20/%3E'));
        });
    </script>
</div>
<script type="text/javascript">var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");
    document.write(unescape("%3Cdiv%20style%3D%27text-align%3Acenter%27%3E%3Cspan id='cnzz_stat_icon_1273638993'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s22.cnzz.com/z_stat.php%3Fid%3D1273638993%26show%3Dpic' type='text/javascript'%3E%3C/script%3E%3C/div%3E"));</script>

<!-- Escape time: 0.053014993667603 --><!-- / DO NOT REMOVE -->

</body>
</html>