• selenium+pyquery爬取淘宝美食100页(无头静默模式)


    import re
    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
    from selenium.common.exceptions import TimeoutException 
    from pyquery import PyQuery as pq
    
    option=webdriver.ChromeOptions()
    option.add_argument('--headless')
    browser = webdriver.Chrome(chrome_options=option)
    
    wait=WebDriverWait(browser, 10)
    
    def search():
        try:
            browser.get("https://www.taobao.com")
      
            input=wait.until(
                EC.presence_of_element_located((By.CSS_SELECTOR, '#q'))
            )
            submit = wait.until(
                    EC.element_to_be_clickable((By.CSS_SELECTOR,'#J_TSearchForm > div.search-button > button')))
            input.send_keys('美食')
            submit.click()
            total=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'#mainsrp-pager > div > div > div > div.total')))
            get_products()
            return total.text
        except TimeoutException:
            return search()
    
    def get_products():
        wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'#mainsrp-itemlist .items .item')))
        html = browser.page_source
        doc = pq(html)
        items = doc('#mainsrp-itemlist .items .item').items() #参见官网http://pyquery.readthedocs.io/en/latest/api.html
        for item in items:
            #以下使用pyquery选择器选择元素
            product = {
                'image':item.find('.pic .img').attr('src'),
                'price':item.find('.price').text().replace('
    ',''),
                'deal':item.find('.deal-cnt').text()[:-3],
                'title':item.find('.title').text().replace('
    ',''),
                'shop':item.find('.shop').text(),
                'location':item.find('.location').text(),
            }
            print(product)
    
    def main():
        total=search()
        total=int(re.compile('(d+)').search(total).group(1))
        for i in range(2,total+1):
            next_page(i)
            
        browser.close()
    
    def next_page(page_number):
        try:
            input=wait.until(
                    EC.presence_of_element_located((By.CSS_SELECTOR, '#mainsrp-pager > div > div > div > div.form > input'))
                )
            submit = wait.until(
                    EC.element_to_be_clickable((By.CSS_SELECTOR,'#mainsrp-pager > div > div > div > div.form > span.btn.J_Submit')))
            input.clear()
            input.send_keys(page_number)
            submit.click()
            wait.until(EC.text_to_be_present_in_element((By.CSS_SELECTOR,'#mainsrp-pager > div > div > div > ul > li.item.active > span'),str(page_number)))
            get_products()
        except TimeoutException:
             next_page(page_number)   
    
    if __name__=='__main__':
        main()
    
    
        
  • 相关阅读:
    java实现操作系统磁盘寻道先来先服务算法
    专业素养:发布文件,别忘了给出校验信息
    vue系列教程-08vue的动画和过渡效果
    vue系列教程-07vue动态绑定样式
    vue系列教程-06vue的事件处理
    vue系列教程-05vue常用指令
    vue系列教程-04vue数据处理和页面渲染
    vue系列教程-03vuejs的结构和生命周期
    vue系列教程-01第一个vue程序
    vue系列教程-02什么是mvvm和spa
  • 原文地址:https://www.cnblogs.com/sherlock318/p/9681033.html
Copyright © 2020-2023  润新知