import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# 创建WebDriver实例
driver = webdriver.Chrome()
# 打开快手快分销网页
driver.get("https://cps.kwaixiaodian.com/zone-cps/promoter_square")
# 等待人工输入账号密码
input("请输入账号密码后按回车继续...")
# 桌面上文档的路径 ice Administrator
output_file_path = "C:\\Users\\ice\\Desktop\\daren_info.txt"
# 打开文档准备写入
with open(output_file_path, 'w', encoding='utf-8') as file:
click_count = 0
while True:
try:
# 等待页面加载
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//*[@id='rc-tabs-1-panel-1']/div[5]/div/div/div[2]/div[1]/div[2]/div[1]/div[2]/div[1]/div[2]/div[2]/div[1]/div[2]")))
# 遍历每个达人项
for x in range(1, 11): # x从1循环到10
daren_id_xpath = f"//*[@id='rc-tabs-1-panel-1']/div[5]/div/div/div[2]/div[{x}]/div[2]/div[1]/div[2]/div[1]/div[2]/div[2]/div[1]/div[2]"
watch_num_xpath = f"//*[@id='rc-tabs-1-panel-1']/div[5]/div/div/div[2]/div[{x}]/div[2]/div[2]/div[1]/div[1]"
daren_id_element = driver.find_element(By.XPATH, daren_id_xpath)
watch_num_element = driver.find_element(By.XPATH, watch_num_xpath)
daren_id = daren_id_element.text if daren_id_element else "未找到达人ID"
watch_num = watch_num_element.text if watch_num_element else "未找到看播人数"
if "万" in watch_num: watch_num = float(watch_num.replace("万", "")) * 10000
else: watch_num = int(watch_num)
if watch_num > 6000: # 30日场均看如果大于6千点进去
daren_item_xpath = f"//*[@id='rc-tabs-1-panel-1']/div[5]/div/div/div[2]/div[{x}]/div[2]/div[1]/div[2]"
driver.find_element(By.XPATH, daren_item_xpath).click()
main_window = driver.current_window_handle
windows = driver.window_handles
driver.switch_to.window([w for w in windows if w != main_window][0])
try:
jun_info_element = driver.find_element(By.XPATH, "//*[@id='rc-tabs-0-panel-situation']/div/div[1]/div[2]/div[2]/div[2]/div[2]")
jun_info = jun_info_element.text # 获取场均消费
try: # 获取联系方式
icon_element = driver.find_element(By.XPATH, "//*[@id='root']/section/main/div/div[1]/div[5]/div[1]/span/svg")
icon_element.click()
wait = WebDriverWait(driver, 10)
phone_element = wait.until(EC.presence_of_element_located((By.XPATH, "//*[@id='root']/section/main/div/div[1]/div[5]/div[2]/span[2]")))
wechat_element = wait.until(EC.presence_of_element_located((By.XPATH, "//*[@id='root']/section/main/div/div[1]/div[5]/div[3]/span[2]")))
phone_info = phone_element.text
wechat_info = wechat_element.text
except Exception as e:
# 失败, "without "
phone_info = "without"
wechat_info = "without"
if jun_info != "0-5000":
file.write(f"达人ID: {daren_id}, 30日场均看播人数: {watch_num}, 场均: {jun_info}, 微信: {wechat_info}, 电话: {phone_info}\n")
file.flush() # 确保立即写入文件
except Exception as e:
print(f"在新窗口中获取信息时出现异常:{e}")
# 关闭新窗口并切换回原来的窗口
driver.close()
driver.switch_to.window(main_window)
# 检查是否有下一页
if click_count <= 2:
next_page_button_xpath = f"//*[@id='rc-tabs-1-panel-1']/div[5]/div/div/div[3]/ul/li[9]/button"
elif click_count == 3:
next_page_button_xpath = f"//*[@id='rc-tabs-1-panel-1']/div[5]/div/div/div[3]/ul/li[10]/button"
else:
next_page_button_xpath = f"//*[@id='rc-tabs-1-panel-1']/div[5]/div/div/div[3]/ul/li[11]/button"
next_page_button = driver.find_element(By.XPATH, next_page_button_xpath)
if not next_page_button.is_enabled():
break # 如果下一页按钮不可用,则退出循环
# 点击下一页
next_page_button.click()
click_count += 1 # 增加点击次数
time.sleep(3) # 等待页面加载
except Exception as e:
print(f"出现异常:{e}")
break # 在出现异常时退出循环
# 关闭浏览器
driver.quit()