|
'''
|
|
作者id author_id; 作者url author_url;
|
|
发布时间 text_time; 内容 text_content; 微博url text_url; 微博/评论 text_type;
|
|
点赞数 like_num; 评论数 comment_num;
|
|
'''
|
|
from selenium import webdriver
|
|
import pandas as pd
|
|
import selenium.webdriver.support.ui as ui
|
|
import time
|
|
import re
|
|
import os
|
|
|
|
|
|
|
|
def login():
|
|
driver.get('https://weibo.com/login.php')
|
|
driver.maximize_window()
|
|
time.sleep(3)
|
|
title = driver.title
|
|
print(title)
|
|
while (title != "微博 – 随时随地发现新鲜事"):
|
|
time.sleep(1)
|
|
title = driver.title
|
|
print(title)
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
driver = webdriver.Chrome()
|
|
wait = ui.WebDriverWait(driver, 10)
|
|
kk = re.compile(r'\d+')
|
|
login()
|
|
|
|
|
|
for i in range(28):
|
|
|
|
csv_name = "weibo_case" + str(i) + ".csv"
|
|
print(csv_name)
|
|
if (not os.path.exists(csv_name)):
|
|
continue
|
|
pd_data = pd.read_csv(csv_name, encoding="utf-8")
|
|
urls = pd_data["text_url"].tolist()
|
|
comments = pd_data["comment_num"].tolist()
|
|
Num = len(urls) + 1
|
|
fp = open(csv_name, 'a', encoding='utf-8')
|
|
|
|
for k in range(len(urls)):
|
|
print(k, "_of_", len(urls))
|
|
time.sleep(1)
|
|
|
|
if (comments[k]=="评论"):
|
|
continue
|
|
|
|
|
|
url_name = urls[k]
|
|
driver.get(url_name)
|
|
time.sleep(2)
|
|
|
|
author_id = []
|
|
text_comment = []
|
|
no_fresh = 0
|
|
for i in range(2000):
|
|
|
|
div_list = driver.find_elements_by_xpath('//*[@id="scroller"]/div[1]/div')
|
|
no_fresh += 1
|
|
for div in div_list:
|
|
_time = div.find_element_by_xpath('./div/div/div/div[1]/div[2]/div[2]/div[1]').text
|
|
name = div.find_element_by_xpath('./div/div/div/div[1]/div[2]/div[1]/a[1]').text
|
|
aurl = div.find_element_by_xpath('./div/div/div/div[1]/div[2]/div[1]/a[1]').get_attribute('href')
|
|
comment = div.find_element_by_xpath('./div/div/div/div[1]/div[2]/div[1]/span').text
|
|
comment = comment.replace(',', ',')
|
|
if ((name in author_id) and (comment in text_comment) or (len(name)<=1)):
|
|
|
|
continue
|
|
|
|
ele = div.find_elements_by_xpath('./div/div/div/div[1]/div[2]/div[2]/div[2]/div[4]/button/span[2]')
|
|
if (len(ele) == 1):
|
|
like = ele[0].text
|
|
else:
|
|
like = 0
|
|
|
|
ele = div.find_elements_by_xpath('./div/div/div/div[2]/div/div/div/span')
|
|
reply = 0
|
|
if (len(ele) == 1):
|
|
x = re.findall(kk, ele[0].text)
|
|
if (len(x) == 1):
|
|
reply = int(x[0])
|
|
|
|
print("No. ", Num, "(", k, "_of_", len(urls), ")")
|
|
print("Time:", _time)
|
|
print("Name:", name)
|
|
print("Comment:", comment)
|
|
print("Like:", like)
|
|
print("Reply:", reply)
|
|
|
|
|
|
text_comment.append(comment)
|
|
author_id.append(name)
|
|
Num += 1
|
|
no_fresh = 0
|
|
fp.writelines(name + "," + aurl + "," + _time + "," + comment + ",,2," + str(like) + "," + str(reply) + "\n")
|
|
|
|
if (no_fresh>=5):
|
|
break
|
|
else:
|
|
driver.execute_script("window.scrollBy(0,500)")
|
|
time.sleep(2)
|
|
|
|
|