• 爬取某人的微博信息


     1 import urllib.request
     2 import json
     3 
     4 #定义要爬取的微博大V的微博ID
     5 id='3995218983'
     6 
     7 #设置代理IP
     8 proxy_addr="122.241.72.191:808"
     9 
    10 #定义页面打开函数
    11 def use_proxy(url,proxy_addr):
    12     req=urllib.request.Request(url)
    13     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")
    14     proxy=urllib.request.ProxyHandler({'http':proxy_addr})
    15     opener=urllib.request.build_opener(proxy,urllib.request.HTTPHandler)
    16     urllib.request.install_opener(opener)
    17     data=urllib.request.urlopen(req).read().decode('utf-8','ignore')
    18     return data
    19 
    20 #获取微博主页的containerid,爬取微博内容时需要此id
    21 def get_containerid(url):
    22     data=use_proxy(url,proxy_addr)
    23     content=json.loads(data).get('data')
    24     for data in content.get('tabsInfo').get('tabs'):
    25         if(data.get('tab_type')=='weibo'):
    26             containerid=data.get('containerid')
    27     return containerid
    28 
    29 #获取微博大V账号的用户基本信息,如:微博昵称、微博地址、微博头像、关注人数、粉丝数、性别、等级等
    30 def get_userInfo(id):
    31     url='https://m.weibo.cn/api/container/getIndex?type=uid&value='+id
    32     data=use_proxy(url,proxy_addr)
    33     content=json.loads(data).get('data')
    34     profile_image_url=content.get('userInfo').get('profile_image_url')
    35     description=content.get('userInfo').get('description')
    36     profile_url=content.get('userInfo').get('profile_url')
    37     verified=content.get('userInfo').get('verified')
    38     guanzhu=content.get('userInfo').get('follow_count')
    39     name=content.get('userInfo').get('screen_name')
    40     fensi=content.get('userInfo').get('followers_count')
    41     gender=content.get('userInfo').get('gender')
    42     urank=content.get('userInfo').get('urank')
    43     print("微博昵称:"+name+"
    "+"微博主页地址:"+profile_url+"
    "+"微博头像地址:"+profile_image_url+"
    "+"是否认证:"+str(verified)+"
    "+"微博说明:"+description+"
    "+"关注人数:"+str(guanzhu)+"
    "+"粉丝数:"+str(fensi)+"
    "+"性别:"+gender+"
    "+"微博等级:"+str(urank)+"
    ")
    44 
    45 
    46 #获取微博内容信息,并保存到文本中,内容包括:每条微博的内容、微博详情页面地址、点赞数、评论数、转发数等
    47 def get_weibo(id,file):
    48     i=1
    49     while True:
    50         url='https://m.weibo.cn/api/container/getIndex?type=uid&value='+id
    51         weibo_url='https://m.weibo.cn/api/container/getIndex?type=uid&value='+id+'&containerid='+get_containerid(url)+'&page='+str(i)
    52         try:
    53             data=use_proxy(weibo_url,proxy_addr)
    54             content=json.loads(data).get('data')
    55             cards=content.get('cards')
    56             if(len(cards)>0):
    57                 for j in range(len(cards)):
    58                     print("-----正在爬取第"+str(i)+"页,第"+str(j)+"条微博------")
    59                     card_type=cards[j].get('card_type')
    60                     if(card_type==9):
    61                         mblog=cards[j].get('mblog')
    62                         attitudes_count=mblog.get('attitudes_count')
    63                         comments_count=mblog.get('comments_count')
    64                         created_at=mblog.get('created_at')
    65                         reposts_count=mblog.get('reposts_count')
    66                         scheme=cards[j].get('scheme')
    67                         text=mblog.get('text')
    68                         with open(file,'a',encoding='utf-8') as fh:
    69                             fh.write("----第"+str(i)+"页,第"+str(j)+"条微博----"+"
    ")
    70                             fh.write("微博地址:"+str(scheme)+"
    "+"发布时间:"+str(created_at)+"
    "+"微博内容:"+text+"
    "+"点赞数:"+str(attitudes_count)+"
    "+"评论数:"+str(comments_count)+"
    "+"转发数:"+str(reposts_count)+"
    ")
    71                 i+=1
    72             else:
    73                 break
    74         except Exception as e:
    75             print(e)
    76             pass
    77 
    78 if __name__=="__main__":
    79     file=id+".txt"
    80     get_userInfo(id)
    81     get_weibo(id,file)
  • 相关阅读:
    1,JAVA图形
    作业(2)
    作业(1)
    作业
    2.15 (第二次作业)
    60页2.6 (第二次作业)
    1.12 (第一次作业)
    27页1.8(第一次作业)
    26页1.3(第一次作业)
    作业第六次
  • 原文地址:https://www.cnblogs.com/chengchengaqin/p/9815057.html
Copyright © 2020-2023  润新知