爬虫实战8:爬取微博内容信息

获取微博大V账号的用户基本信息,如:微博昵称、微博地址、微博头像、关注人数、粉丝数、性别、等级等

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

id = '1259110474'
proxy_addr = "122.241.72.191:808"


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')
    # print(data)
    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):
    global name
    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 = id + ".txt"
    get_userInfo(id)
    get_weibo(id, file)

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
抱歉,我是一个语言模型AI,我没有办法访问互联网上的数据,也没有办法编写爬虫脚本。但是,我可以为你提供一个思路和步骤,帮助你编写一个Python爬虫来实现这个功能。 1. 打开浏览器,访问微博网站,并搜索关键词“如果历史是一群喵”。 2. 找到与关键词相关的微博帖子,并获取其链接。 3. 进入微博帖子页面,找到所有的动漫图片,并获取它们的链接。 4. 下载这些图片到本地。 下面是一个简单的爬虫脚本示例(仅供参考): ```python import requests from bs4 import BeautifulSoup import os # 设置要爬取的页面链接 url = 'https://s.weibo.com/weibo?q=%E5%A6%82%E6%9E%9C%E5%8E%86%E5%8F%B2%E6%98%AF%E4%B8%80%E7%BE%A4%E5%96%B5&Refer=SWeibo_box' # 发送请求,获取页面内容 response = requests.get(url) # 解析页面内容 soup = BeautifulSoup(response.content, 'html.parser') # 找到微博帖子的链接 weibo_links = soup.find_all('a', attrs={'class': 's-btn-c'}) # 遍历每个微博帖子,获取其中的图片链接,并下载图片 for link in weibo_links: # 获取微博帖子链接 weibo_url = link['href'] # 发送请求,获取微博帖子页面内容 weibo_response = requests.get(weibo_url) weibo_soup = BeautifulSoup(weibo_response.content, 'html.parser') # 找到所有的图片链接 img_links = weibo_soup.find_all('img', attrs={'class': 'bigcursor'}) # 遍历每个图片链接,下载图片到本地 for i, img_link in enumerate(img_links): img_url = img_link['src'] img_response = requests.get(img_url) # 创建文件夹 if not os.path.exists('images'): os.mkdir('images') # 下载图片并保存到本地 with open(f'images/{i}.jpg', 'wb') as f: f.write(img_response.content) print('图片下载完成!') ``` 在这个示例中,我们使用了requests库和BeautifulSoup库来获取和解析网页内容,然后遍历每个微博帖子,获取其中的动漫图片链接,并将这些图片下载到本地的images文件夹中。请注意,这个示例只是一个简单的爬虫脚本,实际应用中可能需要考虑更多的异常情况和反爬虫机制。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值