selenium 模拟登录淘宝
import time
from bs4 import BeautifulSoupfrom selenium import webdriverfrom selenium.webdriver.support.ui import WebDriverWait# http://npm.taobao.org/mirrors/chromedriver/class SimulatedTaobaoLogin(): def __init__(self): self.url = "https://www.taobao.com" self.options = webdriver.ChromeOptions() self.options.add_argument("--disable-blink-features=AutomationControlled") self.broswer = webdriver.Chrome(executable_path=r"C:\Users\Administrator\Desktop\mid\spiders\chromedriver", options=self.options) self.maxwindow = self.broswer.maximize_window() # 最大化窗口 self.wait = WebDriverWait(self.broswer, 5) # 显式等待<br> # 普通账号密码登录 以及 cookie 获取 def cookieGet(self) -> str: self.broswer.get('https://login.taobao.com/member/login.jhtml?spm=a21bo.jianhua.754894437.1.5af911d9bYhkW7&f=top&redirectURL=https%3A%2F%2Fwww.taobao.com%2F') self.broswer.implicitly_wait(3) user = self.broswer.find_element_by_xpath('//input[@id="fm-login-id"]') user.click() user.clear() time.sleep(3) user.send_keys('userName') # 输入你的账号 time.sleep(5) pwd = self.broswer.find_element_by_xpath('//input[@id="fm-login-password"]') pwd.click() pwd.clear() time.sleep(3) pwd.send_keys("userPwd") # 输入你的密码 time.sleep(5) login = self.broswer.find_element_by_xpath('//button') login.click() time.sleep(30) cookie = self.broswer.get_cookies() return cookie # cookie 登录 def cookieLogin(self, cookies:str) -> None: self.broswer.get(self.url) time.sleep(1) self.broswer.delete_all_cookies() # 清除原有的cookie信息 for cookie in cookies: self.broswer.add_cookie(cookie) time.sleep(3) self.broswer.refresh() # 刷新 time.sleep(5) search_input = self.broswer.find_element_by_xpath('//*[@id="q"]') shop_name = '月饼' search_input.send_keys(shop_name) time.sleep(5) search_submit = self.broswer.find_element_by_xpath('//*[@id="J_TSearchForm"]/div[1]/button') search_submit.click() time.sleep(5) def __call__(self, *args, **kwargs): cookies = self.cookieGet() self.cookieLogin(cookies) self.broswer.quit()if __name__ == '__main__': simulatedTaobaoLogin = SimulatedTaobaoLogin() simulatedTaobaoLogin()

浙公网安备 33010602011771号