Python爬蟲——批量爬取微博圖片(不使用cookie)

引言:剛開始我想要爬取微博的照片,但是發現網上大多數的blog都是需要一個cookie的東西,當時我很難得到,偶然翻到一個個人的技術博客:
http://www.omegaxyz.com/2018/02/13/python_weibo/
(順便推薦一下他的博客吧,裏面有很多幹貨)))
試了一下,效果還是很好的:

# -*- coding: utf-8 -*-
import urllib.request
import json
import requests
import os

path = 'D:\\picture\\weibo\\'

#id = '2093492691'
id = '2374240301'
proxy_addr = "122.241.72.191:808"
pic_num = 0
weibo_name = "programmer"


def use_proxy(url, proxy_addr):
    req = urllib.request.Request(url)
    req.add_header("User-Agent",
                   "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0")
    proxy = urllib.request.ProxyHandler({'http': proxy_addr})
    opener = urllib.request.build_opener(proxy, urllib.request.HTTPHandler)
    urllib.request.install_opener(opener)
    data = urllib.request.urlopen(req).read().decode('utf-8', 'ignore')
    return data


def get_containerid(url):
    data = use_proxy(url, proxy_addr)
    content = json.loads(data).get('data')
    for data in content.get('tabsInfo').get('tabs'):
        if (data.get('tab_type') == 'weibo'):
            containerid = data.get('containerid')
    return containerid


def get_userInfo(id):
    url = 'https://m.weibo.cn/api/container/getIndex?type=uid&value=' + id
    data = use_proxy(url, proxy_addr)
    content = json.loads(data).get('data')
    profile_image_url = content.get('userInfo').get('profile_image_url')
    description = content.get('userInfo').get('description')
    profile_url = content.get('userInfo').get('profile_url')
    verified = content.get('userInfo').get('verified')
    guanzhu = content.get('userInfo').get('follow_count')
    name = content.get('userInfo').get('screen_name')
    fensi = content.get('userInfo').get('followers_count')
    gender = content.get('userInfo').get('gender')
    urank = content.get('userInfo').get('urank')
    print("微博暱稱:" + name + "\n" + "微博主頁地址:" + profile_url + "\n" + "微博頭像地址:" + profile_image_url + "\n" + "是否認證:" + str(
        verified) + "\n" + "微博說明:" + description + "\n" + "關注人數:" + str(guanzhu) + "\n" + "粉絲數:" + str(
        fensi) + "\n" + "性別:" + gender + "\n" + "微博等級:" + str(urank) + "\n")


def get_weibo(id, file):
    global pic_num
    i = 1
    while True:
        url = 'https://m.weibo.cn/api/container/getIndex?type=uid&value=' + id
        weibo_url = 'https://m.weibo.cn/api/container/getIndex?type=uid&value=' + id + '&containerid=' + get_containerid(
            url) + '&page=' + str(i)
        try:
            data = use_proxy(weibo_url, proxy_addr)
            content = json.loads(data).get('data')
            cards = content.get('cards')
            if (len(cards) > 0):
                for j in range(len(cards)):
                    print("-----正在爬取第" + str(i) + "頁,第" + str(j) + "條微博------")
                    card_type = cards[j].get('card_type')
                    if (card_type == 9):
                        mblog = cards[j].get('mblog')
                        attitudes_count = mblog.get('attitudes_count')
                        comments_count = mblog.get('comments_count')
                        created_at = mblog.get('created_at')
                        reposts_count = mblog.get('reposts_count')
                        scheme = cards[j].get('scheme')
                        text = mblog.get('text')
                        if mblog.get('pics') != None:
                            # print(mblog.get('original_pic'))
                            # print(mblog.get('pics'))
                            pic_archive = mblog.get('pics')
                            for _ in range(len(pic_archive)):
                                pic_num += 1
                                print(pic_archive[_]['large']['url'])
                                imgurl = pic_archive[_]['large']['url']
                                img = requests.get(imgurl)
                                f = open(path + weibo_name + '\\' + str(pic_num) + str(imgurl[-4:]),
                                         'ab')  # 存儲圖片,多媒體文件需要參數b(二進制文件)
                                f.write(img.content)  # 多媒體存儲content
                                f.close()

                        with open(file, 'a', encoding='utf-8') as fh:
                            fh.write("----第" + str(i) + "頁,第" + str(j) + "條微博----" + "\n")
                            fh.write("微博地址:" + str(scheme) + "\n" + "發佈時間:" + str(
                                created_at) + "\n" + "微博內容:" + text + "\n" + "點贊數:" + str(
                                attitudes_count) + "\n" + "評論數:" + str(comments_count) + "\n" + "轉發數:" + str(
                                reposts_count) + "\n")
                i += 1
            else:
                break
        except Exception as e:
            print(e)
            pass


if __name__ == "__main__":
    if os.path.isdir(path + weibo_name):
        pass
    else:
        os.mkdir(path + weibo_name)
    file = path + weibo_name + '\\' + weibo_name + ".txt"
    get_userInfo(id)
    get_weibo(id, file)

這裏唯一需要的一個user的id,id = '2374240301’這一行,可以自己修改,關於怎麼獲取user的id,也很簡單:
從微博中打開一個博主的微博:

在這裏插入圖片描述看一下右上角的地址欄:
上面的這個圖片就是:
https://weibo.com/u/1658996247?refer_flag=1087030701_2975_2003_0&is_hot=1

其中1658996247就是usr的id了,把這個粘貼過去就好,運行代碼,可以看到我們的路徑裏爬取的圖片:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章