from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
import time
def get_series_hotness(url):
# 初始化Chrome浏览器(无头模式,不显示界面,可注释掉增加可视化)
options = webdriver.ChromeOptions()
# options.add_argument("--headless") # 无头模式,注释掉可看到浏览器操作
options.add_argument("--disable-blink-features=AutomationControlled") # 规避部分反爬
options.add_user_profile_directory = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
# 自动安装并启动Chrome驱动
driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
try:
driver.get(url) # 打开目标页面
time.sleep(3) # 等待页面加载(可根据网络情况调整)
# 定位热度元素(需根据实际页面的HTML结构修改选择器)
# 示例:假设热度在class为"video-hot"的div中
hotness_element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, ".video-hot")) # 替换为实际的CSS选择器
)
hotness = hotness_element.text.strip()
print(f"《天地剑心》当前热度:{hotness}")
return hotness
except Exception as e:
print(f"爬取失败:{e}")
return None
finally:
driver.quit() # 关闭浏览器
# 替换为《天地剑心》在腾讯视频的实际播放页面URL
series_url = "https://v.qq.com/x/cover/xxxxxx.html" # 示例URL,需替换
get_series_hotness(series_url)