Selenium+Python3爬取微博我发出的评论信息
需求
- 记录对话信息:对话文本、时间、用户、被回复链接、被回复用户、被回复文本。
- 将数据信息持久化保存,可选择截图。
代码
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"
MY_COMMENT_PATH = r"\我的评论"
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 = {
}
users_for_this_time = []
problem_users = []
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