Selenium例子-1

来自通约智库
笑笑讨论 | 贡献2023年3月11日 (六) 23:58的版本
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航搜索

from selenium.webdriver.common.by import By import time import re

def search_product():

   driver.find_element(By.XPATH, ('//*[@id="q"]')).send_keys(kw)
   driver.find_element(By.XPATH, ('//*[@id="J_TSearchForm"]/div[1]/button')).click()
   time.sleep(10)
   page = driver.find_elements(By.XPATH, ('//*[@id="mainsrp-pager"]/div/div/div/div[1]')).text
   page = int(re.compile('(\d+)').search(page).group(1))
   return page

def drop_down():

   for x in range(1,11,2):
       time.sleep(0.5)
       j = x/10
       js = 'document.documentElement.scrollTop = document.documentElement.scrollHeight * %f'%j
       driver.execute_script(js)

def get_product():

   divs = driver.find_elements(By.XPATH,('//*[@id="mainsrp-itemlist"]/div/div/div[1]/div[1]'))
   for div in divs:
       picture = div.find_elements(By.XPATH,('.//div[@class="pic"]/a/img')).get_attribute('src')
       introduce = div.find_elements(By.XPATH,('.//div[@class="price g_price g_price-highlight"]/strong')).text
       price = div.find_elements(By.XPATH, ('.//div[@class="row row-2 title"]/a')).get_attribute('trace-price') + '元'
       sales = div.find_elements(By.XPATH,('.//div[@class="deal-cnt"]')).text
       name = div.find_elements(By.XPATH, ('.//div[@class ="row row-2 title"]/a/span[2]')).text
       place = div.find_elements(By.XPATH, ('.//div[@class="location"]')).text
       product = {'图片':picture,'介绍':introduce,'价格':price,'销售':sales,'名字':name,'地点':place}
       print(product)


def next_page():

   page = search_product()
   drop_down()
   get_product()
   num = 1
   while num != page:
       driver.get('https://s.taobao.com/search?q={}&s={}'.format(kw, 44*num))
       driver.implicitly_wait(10)
       num += 1
       drop_down()
       get_product()


if __name__ == '__main__':

   kw = input('请输入你想搜索的商品:')
   options = webdriver.ChromeOptions()
   options.add_experimental_option("detach", True)
   driver = webdriver.Chrome(options=options)
   driver.get('https://www.taobao.com/')
   next_page()



来自:[1]