python 新浪微博爬虫

葫芦的运维日志

下一篇 搜索 上一篇

浏览量 4466

2018/07/01 12:43


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/7/1 上午3:29
# @Author  : BrownWang
# @Email   : 277215243@qq.com
# @File    : sinaweibo.py
# @Software: PyCharm

# -*- coding: utf-8 -*-

import urllib.request
import json
import time

import ssl
context = ssl._create_unverified_context()
#定义要爬取的微博大V的微博ID
id='6418190932'

#设置代理IP
proxy_addr="122.241.72.191:808"
#定义页面打开函数
def use_proxy(url,proxy_addr):
    req=urllib.request.Request(url,unverifiable=True)
    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,context=context).read().decode('utf-8','ignore')
    return data

#获取微博主页的containerid,爬取微博内容时需要此id
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

#获取微博大V账号的用户基本信息,如:微博昵称、微博地址、微博头像、关注人数、粉丝数、性别、等级等
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):
    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')
                        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__":

    file=time.strftime("%Y-%m-%d-%H", time.localtime())+"-"+id+".txt"
    get_userInfo(id)
    get_weibo(id,file)




"""
微博昵称:鸟叔操盘--指导
微博主页地址:https://m.weibo.cn/u/6418190932?uid=6418190932&luicode=10000011&lfid=1005056418190932
微博头像地址:https://tvax2.sinaimg.cn/crop.0.0.180.180.180/0070m4EAly8fpp1pwqq26j3050050jrb.jpg
是否认证:False
微博说明:知名财经博主 头条文章作者 微博签约自媒体
关注人数:3301
粉丝数:111
性别:m
微博等级:11

-----正在爬取第1页,第0条微博------
-----正在爬取第1页,第1条微博------
-----正在爬取第1页,第2条微博------
-----正在爬取第1页,第3条微博------
-----正在爬取第1页,第4条微博------
-----正在爬取第1页,第5条微博------
-----正在爬取第1页,第6条微博------
-----正在爬取第1页,第7条微博------
-----正在爬取第1页,第8条微博------
-----正在爬取第2页,第0条微博------
-----正在爬取第2页,第1条微博------
-----正在爬取第2页,第2条微博------
-----正在爬取第2页,第3条微博------
"""


"""
----第1页,第0条微博----
微博地址:https://m.weibo.cn/status/GyHojrupo?mblogid=GyHojrupo&luicode=10000011&lfid=1076036418190932
发布时间:09-10
微博内容:9-10午盘:创业板再创新低,前期的最强的板块,科技股压制大盘反弹,<br />盘面不清晰方向,尾盘再决定买股方向。 ​​​
点赞数:0
评论数:0
转发数:0
----第1页,第1条微博----
微博地址:https://m.weibo.cn/status/GyHogu2PR?mblogid=GyHogu2PR&luicode=10000011&lfid=1076036418190932
发布时间:09-10
微博内容:这波反弹的动力来源于权重股 ​​​
点赞数:0
评论数:0
转发数:0
----第1页,第2条微博----
微博地址:https://m.weibo.cn/status/GyHof47S5?mblogid=GyHof47S5&luicode=10000011&lfid=1076036418190932
发布时间:09-10
微博内容:我认为浪潮信息是没有多大的回调空间的,贸易战的真皮还没有披露,依然还停留在贸易层次,升级到科技层面还尚需要时间。美国对中国的优势就是科技,除了科技,它没有多少牌能够压制中国。<br />中国的绝对优势就是:市场那么大,摆在那里,任何人都不可忽略。 ​​​
点赞数:0
评论数:0
转发数:0
----第1页,第3条微博----
微博地址:https://m.weibo.cn/status/GyHocDf15?mblogid=GyHocDf15&luicode=10000011&lfid=1076036418190932
发布时间:09-10
微博内容:东方财富不是科技股,它只是创业板的权重股,它的前期已经够抗跌于创业板了,那么创业板继续创新低,它无可避免是要跟随回调的,这是东财今天迅速跌停的原因。 ​​​
点赞数:0
评论数:0
转发数:0
"""

 

葫芦的运维日志

打赏

上一篇 搜索 下一篇
© 冰糖葫芦甜(bthlt.com) 2021 王梓打赏联系方式 陕ICP备17005322号-1