需要:谷歌浏览器、chromedriver(对应浏览器版本的)
import time
from selenium import webdriver
#可替换成自己需要监测的那条微博网址
url='https://weibo.com/2683843043/GtH3L1STV?from=page_1006062683843043_profile&wvr=6&mod=weibotime&type=comment#_rnd1533917518297'
def start_chrome():
driver = webdriver.Chrome(executable_path='./chromedriver.exe')
driver.start_client()
return driver
def find_info():
#CSS路径,表示对应到需要监测的数据
sel='span>span.line.S_line1>span>em:nth-child(2)'
#网页元素定位并提取
elems=driver.find_elements_by_css_selector(sel)
return [int(el.text) for el in elems[1:]]
while True:
driver=start_chrome()
driver.get(url)
time.sleep(6)#浏览器6秒缓冲时间
info = find_info()
#info中的参数用以下三个参数表示
rep,comm,like = info
#如果转发量超过50000,则打印以下
if rep>50000:
print(f'你喜欢的微博转发量已经达到了{rep}')
break
else:
print('啥都没发生')
time.sleep(1200)#每1200秒循环一次
print('Done!')