from selenium import webdriver标签:__,index,self,笔记,element,find,browser From: https://www.cnblogs.com/fuchenjie/p/17724640.html
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class Tiktok:
def __init__(self):
# 创建浏览器实例
self.browser = webdriver.Chrome()
self.user_info = []
def get_fllow_info(self):
"""获取已关注主播信息"""
fllow_div = self.browser.find_element(By.XPATH, "//div[@id='main-content-homepage_follow']")
card_divs = fllow_div.find_elements(By.XPATH, "//div[@data-e2e='recommend-card']")
for index, item in enumerate(card_divs):
# 获取主播主页地址、姓名
index_url = item.find_element(By.XPATH, "a").get_attribute("href")
user_name = item.find_elements(By.XPATH, "//h3")[index].text
self.user_info.append({"name": user_name, "index_url": index_url})
def run(self):
# 打开网页
self.browser.get('https://www.tiktok.com/')
# 进入我的关注页面
element = WebDriverWait(self.browser, 120, 1).until(
EC.presence_of_element_located((By.XPATH, "//a[@data-e2e='nav-following']"))
)
element.click()
# 获取关注的主播信息
self.get_fllow_info()
# 查找搜索按钮并单击
search_button = browser.find_element_by_id('su')
search_button.click()
# 关闭浏览器
browser.quit()
if __name__ == "__main__":
tiktok = Tiktok()
tiktok.run()