4th Street Bar Hive-Bar

Hive-Bar Powered by Hive beta-fdb5b5b

Community post

微博爬虫制作教程(小白版)#30分钟搞定微博爬虫系列

要获取微博数据,最简单是就是模仿浏览器或者客户端来调用API接口获取数据。而这些API往往是没有文档的,所以只有自己抓取分析。

  1. 首先,打开chrome
  2. 然后F12,打开开发人员面板
  3. 访问http://m.weibo.cn/u/1378236401
  4. 切换到network选项卡后点击过滤器中的xhr,并把网页向下滚动几页

img

点击最底下的那条流量查看流量详情

img

  红色框内即为你提交的数据,切换到preview选项卡即可查看服务器返回的数据。

img

img

可以看到访问这个链接(API)得到的回馈为json数据,且为我们所需要的博客内容。

如图可见返回数据的['cards']字段内的list是我们想要的数据,再经过实验,确定card['card_type'] == 11时是删除的微博,因此无需显示。

然后就是构造请求模拟header开始爬取了,完整代码如下:

from requests import get
from time import sleep
import json

# header设置部分
headers = '''
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Upgrade-Insecure-Requests: 1
User-Agent: xxx
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: gzip, deflate, sdch
Accept-Language: zh-CN,zh;q=0.8
Cookie: xxxxx
'''

headers = headers.split('\n')
headers = [x.split(': ') for x in headers if x is not '']
headers = {x[0]: x[1] for x in headers}

# API调用参数设置部分
uid = int('1378236401')
containerid = int('1076031378236401')
pageStart = int('1')

pageInfo = {"sudaref": "m.weibo.com", "type": "uid", "value": uid,
            "containerid": containerid, "page": pageStart}

# 从API获取失败重试次数
retry = 0


def get_json_data(pageNum):
    global retry
    global pageInfo
    global headers
    pageInfo['page'] = str(pageNum)
    content = get('http://m.weibo.cn/container/getIndex', params=pageInfo,
                  headers=headers).content
    json_data = json.loads(content.decode())
    if json_data['ok']:
        retry = 0
        return json_data
    elif retry < 3:
        print(json.dumps(json_data))
        retry += 1
        return get_json_data(pageNum)
    else:
        return json_data


for page_num in range(pageStart, 999):
    ret_data = get_json_data(page_num)
    try:
        for card in ret_data['cards']:
            if card['card_type'] == 11:
                continue
            print(card['mblog']['text'])
    except:
        open('debug.log', 'ab+').write(str(json.dumps(ret_data)).encode())
        print(ret_data)
        print('\033[1;31;0m数据获取错误,请检测网络')
        exit(1)
    if ret_data['cardlistInfo']['page'] is None:
        print('\033[1;32;0m好像已经爬完了,总共{0}页微博数据'.format(page_num))
        exit()
    sleep(1)

从自己浏览器请求headers里拷贝出来并自行设置一下上面的headers字段以及对应填好你要爬的用户id开始页数啥的就好。

46 upvotes $1.43

Replies (24)

Review before signing

Posting as . Signing with . Keychain permission: Posting. Hive Keychain will ask you to approve this action next.


  
Technical details

Operation fingerprint: