Selenium+Python3爬取微博我发出的评论信息

Selenium+Python3爬取微博我发出的评论信息

需求

  1. 记录对话信息:对话文本、时间、用户、被回复链接、被回复用户、被回复文本。
  2. 将数据信息持久化保存,可选择截图。

代码

# coding:utf8

import datetime
import os
import re
import time

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.remote import webelement
from selenium.webdriver.support.wait import WebDriverWait

# ------------------------------
#   全局变量定义
# ------------------------------

SAVE_PATH = r"F:\xx"  # main directory
MY_COMMENT_PATH = r"\我的评论"  # comment directory

# year and date of target,last_date_str晚于first_date_str
year_str, last_date_str, first_date_str = datetime.datetime.today().strftime('%Y'), '11-01', '09-08'

# 是否截图
IS_SCREEN_SHOT = False


# ------------------------------
#   类定义
# ------------------------------


class wb_user:
    """
    微博用户
    """
    name = code = ''

    def __init__(self, name, code):
        self.code, self.name = code, name

    def __str__(self):
        return self.name + '\t' + self.code

    def __eq__(self, o):
        return type(self) == type(o) and self.code == o.code and self.name == o.name


me = None  # 登录者对象
name_o_dict = {
   }  # 保存用户名、id键值对
users_for_this_time = []  # 此次出现的用户,用于统计
problem_users = []  # 取id有问题的用户


class a_comment:
    """
    单条评论中的信息:评论文本、时间、用户、被回复链接、被回复用户、被回复文本
    """
    c_str = c_time = c_user = bc_link = bc_user = bc_str = None

    def __init__(self, c_str, c_time, c_user, bc_link, bc_user, bc_str):
        """
        :param c_str: 评论文本
        :param c_time: 时间
        :param c_user: 评论用户
        :param bc_link: 被回复链接
        :param bc_user: 被回复用户
        :param bc_str: 被回复文本
        """
        self.c_str, self.c_time, self.c_user, self.bc_link, self.bc_user, self.bc_str = \
            c_str, c_time, c_user, bc_link, bc_user, bc_str

    def __eq__(self, o):
        attributes = [a for a in dir(self) if a.startswith('c_') or a.startswith('bc_')]
        if type(o) != type(self):
            return False
        for a in attributes:
            if self.__getattribute__(a) != o.__getattribute__(a):
                return False
        return True

    def __str__(self):
        return '\t'.join([str(self.c_user), self.c_str.replace('\n', '\\n').replace('\t', '\\t'),
                          self.c_time.strf
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值