import selenium 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 import time import json import random def grab_herf(driver): herf_list = driver.find_elements(By.CSS_SELECTOR, "a.cover.mask.ld") herf_result = [] for herf in herf_list: herf_result.append(herf.get_attribute("href")) return herf_result def scroll_page(driver): driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") time.sleep(random.random()*2) return True def main(): driver = webdriver.Safari() # Url = "https://www.xiaohongshu.com/search_result?keyword=%25E6%2597%2585%25E6%25B8%25B8%25E6%2594%25BB%25E7%2595%25A5&source=web_explore_feed" # Url = "https://www.xiaohongshu.com/user/profile/5b0c19d2e8ac2b4f75c6cad2?xsec_token=ABTQo4AIGKlpW474JBfIZcd8Ln8DQr1Ugt_Yk-w3h1bzs%3D&xsec_source=pc_search" Url = "https://www.xiaohongshu.com/user/profile/5ac968dbe8ac2b0a5581b909?xsec_token=YBG4U7EYzyAxyWC5H0HQKip9JT9B9KxrA43gWtkA3mi1A%3D&xsec_source=app_share&xhsshare=WeixinSession&appuid=6326ddf20000000023024438&apptime=1741862947&share_id=5da7602111cb431ca55a0dcfd6b20bc3&share_channel=wechat" Url = "https://www.xiaohongshu.com/user/profile/663057ef000000001e0060c9?xsec_token=YB1i6fylSYGpHg1gVgG5KZohqjJvDmwR96wwU_AQxdRrI=&xsec_source=app_share&xhsshare=WeixinSession&appuid=6326ddf20000000023024438&apptime=1741863000&share_id=b53d29eb6ce144c4b2d99035e4369d55&share_channel=wechat&wechatWid=cb745eed2e2630a361f9ce99520d9c9a&wechatOrigin=menu" cookies = json.load(open("/Users/yarrow/Spider/seleniumXH/cookies.json", "r")) for cookie in cookies: cookie['sameSite'] = 'Strict' # 或者根据需要设置为'Strict'或'None' driver.add_cookie(cookie) ## 连接网页 driver.get(Url) driver.maximize_window() ## 等待页面加载 # WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "search_result"))) time.sleep(1.5) ## 最新 tag ##