代码传送门
打开淘宝,并成功模拟登入
模拟登入第一步,TB会被发现自动化控制,所以要不被发现才行,如下:
options = webdriver.ChromeOptions()
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_experimental_option(
'excludeSwitches', ['enable-automation'])
driver = webdriver.Chrome(options=options)
寻找账号密码登入框
寻找登入框各部分,直接输入账号密码自动运行
def Running(user,pswd):
try:
element = driver.find_element(By.XPATH,'//*[@id="fm-login-id"]')
ActionChains(driver).move_to_element(element).perform()
sleep(0.5)
element.click()
element.send_keys(user)
sleep(0.5)
element = driver.find_element(By.XPATH,'//*[@id="fm-login-password"]')
ActionChains(driver).move_to_element(element).perform()
sleep(0.5)
element.click()
element.send_keys(pswd)
sleep(0.5)
element = driver.find_element(By.XPATH,'//button[text()="登录"]').click()
sleep(0.5)
now_handle = driver.current_window_handle
print(now_handle)
currentPageUrl = driver.current_url
print("当前页面的url是:", currentPageUrl)
except Exception as e:
print(e)
切记:只要不多次频繁登入就不会出现扫码环节
寻找搜索框输入所需信息
element.send_keys(‘需要搜索的物品’)
搜素之前判断是否进入主页,看看是否卡在登入界面
def Search(url):
currentPageUrl = driver.current_url
print("当前页面的url是:", currentPageUrl)
driver.get(url)
element = driver.find_element(By.XPATH,'//input[@name="q"]')
element.send_keys('充电宝')
element = driver.find_element(By.XPATH ,'//button[text()="搜索"]').click()
print(element)
找到商品,爬取信息
找到商品后爬取有用信息并保存到Variable Explorer下
def Find_info(hrefs_,imgs_list,texts_list):
sleep(0.5)
for link in driver.find_elements(By.XPATH,'//div[@class="row row-2 title"]/a'):
sleep(0.1)
hrefs_.append(link.get_attribute('href'))
for link in driver.find_elements(By.XPATH,'//div[@class="pic"]/a/img'):
sleep(0.1)
imgs_list .append(link.get_attribute('src'))
for link in driver.find_elements(By.XPATH,'//div[@class="row row-2 title"]/a'):
sleep(0.1)
texts_list.append(link.get_attribute('textContent').strip())
return hrefs_,imgs_list,texts_list
后续设计UI界面,搜索最优物品,保存如csv中…
如果能省下大佬们搜索的时间的话给弟弟一个赞把😘😘😘 嘿嘿!ψ(`?′)ψ
|