from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from urllib.parse import quote

browser = webdriver.Chrome()
wait = WebDriverWait(browser,10)
KEYWORD = 'iPad'

def index_page(page):
	"""
	抓取索引页
	:param page: 页码
	"""
	print('正在爬去第',page,'页')
	try:
		url = 'https://s.taobao.com/search?q=' + quote(KEYWORD)
		browser.get(url)
		if page > 1:
			input = wait.until(
				EC.presence_of_element_located((By.CSS_SELECTOR,'#mainsrp-pager div.form > input')))
			submit = wait.until(
				EC.element_to_be_clickable((By.CSS_SELECTOR,'#mainsrp-pager div.form > span.btn.J_Submit')))

			input.clear()
			input.send_keys(page)
			submit.click()
		wait.until(
			EC.text_to_be_present_in_element_located((By.CSS_SELECTOR,'#mainsrp-pager li.item.active > span'),str(page)))
		wait.until(
			EC.presence_of_element_located((By.CSS_SELECTOR,'.m-itemlist .tems .item')))
		get_products()

	except TimeoutException:
		index_page(page)
