一、总述
微博带带大师兄的无内鬼环节出现的内鬼让很多狗粉丝头痛不已,selenium爬虫自动化下载无内鬼环节评论图片,人工筛选出发鬼图、恶心图的内鬼ID,在下次下载中将内鬼ID加入列表剔除即可。
二、弊端
效率低,表现在手动输入验证码,手动在剔除内鬼ID,加载网页、图片时需等待时间。
三、代码
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
import requests
import math
import os
import datetime
def verifycode():
if wd.find_elements_by_xpath('//*[@src="about:blank"]') == []:
vcode = input('请输入验证码:\n')
wd.find_element_by_xpath('//*[@action-data="text=请输入验证码"]').send_keys(vcode)
def newwindow(word,wd):
for i in wd.window_handles:#选择新的窗口
wd.switch_to.window(i)
if word in wd.title:
break
def findneigui():
element1 = wd.find_elements_by_xpath('.//*[@class="WB_repeat S_line1"]//*[@class="list_con"]')
for i in element1: # 保留:无内鬼
if ':无内鬼 ' in i.get_attribute('outerHTML'):
i.find_element_by_xpath(".//*[contains(text(),'条回复')]").click() # 点击更多评论
break
pass
def ncommits():#得到总评论数
time.sleep(.2)
element2 = wd.find_elements_by_xpath('.//*[@node-type="root_comment"]')
for i in element2: #保留:更多评论
if '更多' in i.get_attribute('outerHTML'):
s = i.find_element_by_xpath("//*[contains(text(),'更多')]").text
a = str.find(s, '多')
b = str.find(s, '条')
c = int(s[(a + 1):b])
break
return c
def clickmore():
j = 1#设定循环初始值
n = math.ceil(ncommits()/15)+1#点击一次加载评论
while j < n:
if wd.find_elements_by_xpath("//*[contains(text(),'更多') and contains(text(),'条')]") == []:#找不到就退出
break
else:
i = wd.find_element_by_xpath("//*[contains(text(),'更多') and contains(text(),'条')]")
lo = i.location['y']-400
wd.execute_script("document.documentElement.scrollTop= %d" % lo)
time.sleep(.3)
try:
i.click()
time.sleep(.7)
except:
break
j += 1
pass
def getpic():
element3 = wd.find_elements_by_xpath('.//*[@action-type="widget_photoview"]')
m = 0
neigui = ['我主页无内鬼嗷','狗瘦如柴','GuocGg','S就k玩r事狗了']#今天的内鬼名单,可自行添加
for i in element3:
#找到配图位置
lo = i.location['y']-350
if lo < 200:
time.sleep(5)#开始时停5秒
wd.execute_script("document.documentElement.scrollTop= %d" % lo)
#打印发布者信息
e1 = i.find_element_by_xpath('..//*[@usercard]').text
if e1 not in neigui:#内鬼必须死
try:
ActionChains(wd).move_to_element(i).perform()
if m == 0:
time.sleep(3)
time.sleep(.45)
phurl = wd.find_element_by_xpath('//*[@class="W_layer W_layer_pop "]//img').get_attribute('src')
e2 = phurl[(len(phurl)-4):(len(phurl))]
phd = requests.get(phurl)
with open(e1 + str(m) + e2, 'wb') as f:#以发图片id+次序(一个id有发多张图片的可能)+扩展名命名
f.write(phd.content)
m += 1
except:
pass
pass
def login():
wd.find_element_by_xpath('//*[@action-data="text=邮箱/会员帐号/手机号"]').send_keys(' ')#输入用户名
wd.find_element_by_xpath('//*[@class="enter_psw"]/preceding-sibling::*').send_keys(' ')#输入密码
verifycode()##暂时手动输入验证码
time.sleep(1.5)
wd.find_element_by_xpath('//*[@class="info_list login_btn"]/*').click()
time.sleep(2.5)
wd.find_element_by_xpath('//*[@node-type="searchInput"]').send_keys('孙笑川')
wd.find_element_by_xpath('//*[@class="W_ficon ficon_search S_ficon"]').click()
wd.find_element_by_xpath('//*[contains(text(),"带带大师兄")]').click()#进入带带大师兄主页
newwindow('带带大师兄', wd)
wd.find_element_by_xpath('//*[@suda-data="key=tblog_profile_new&value=weibo_all"]').click()#点击全部(微博)
wd.find_element_by_xpath('//*[@feedtype="top"]/../*[3]//*[@action-type="fl_comment"]').click()#点击第一条微博的评论
time.sleep(4)
findneigui()
newwindow('来自带带大师兄', wd)
time.sleep(4)#打开新窗口等待4s加载时间
clickmore()#运行点击更多评论的程序
time.sleep(5)#结束点击后再等待5s
getpic()#下载所有评论图片
pass
if __name__=='__main__':
t1 = datetime.datetime.now()#记录开始时间
os.chdir(r"C:\Users\Desktop\内鬼")#在桌面建立好的文件夹储存图片
wd = webdriver.Chrome(r'D:\python\chromedriver.exe')
wd.implicitly_wait(10)
wd.get('http://weibo.com/')
wd.maximize_window()#窗口最大化,以便取到元素坐标
login()
t2 = datetime.datetime.now()#记录结束时间
print(t2-t1)#打印运行时间
本文介绍了使用selenium爬虫针对微博无内鬼环节的实践,通过自动化下载评论图片,手动筛选内鬼ID,并在后续爬取中排除这些ID,以提高效率。
1887

被折叠的 条评论
为什么被折叠?



