zoukankan      html  css  js  c++  java
  • 爬取淘宝笔记本电脑数据(一)

    爬取淘宝笔记本电脑数据

    1.导入模块

    from selenium import webdriver
    import time
    import csv
    import re
    

    2.搜索商品,获取商品页码

    image-20201009201355074

    image-20201009201535357

    image-20201009201926722

    def search_product(key_word):
        # 定位输入框
        browser.find_element_by_id("q").send_keys(key_word)
        # 定义点击按钮,并点击
        browser.find_element_by_class_name('btn-search').click()
        # 最大化窗口:为了方便我们扫码
        browser.maximize_window()
        # 等待15秒,给足时间我们扫码
        time.sleep(15)
        # 定位这个“页码”,获取“共100页这个文本”
        page_info = browser.find_element_by_xpath('//div[@class="total"]').text
        # 需要注意的是:findall()返回的是一个列表,虽然此时只有一个元素它也是一个列表。
        page = re.findall("(d+)",page_info)[0]
        return page
    

    3.获取商品信息

    image-20201009202238347

    def get_data():
        # 通过页面分析发现:所有的信息都在items节点下
        items = browser.find_elements_by_xpath('//div[@class="items"]/div[@class="item J_MouserOnverReq  "]')
        for item in items:
            # 参数信息
            pro_desc = item.find_element_by_xpath('.//div[@class="row row-2 title"]/a').text
            # 价格
            pro_price = item.find_element_by_xpath('.//strong').text
            # 付款人数
            buy_num = item.find_element_by_xpath('.//div[@class="deal-cnt"]').text
            # 旗舰店
            shop = item.find_element_by_xpath('.//div[@class="shop"]/a').text
            # 发货地
            address = item.find_element_by_xpath('.//div[@class="location"]').text
            #print(pro_desc, pro_price, buy_num, shop, address)
            with open('{}.csv'.format(key_word), mode='a', newline='', encoding='utf-8-sig') as f:
                csv_writer = csv.writer(f, delimiter=',')
                csv_writer.writerow([pro_desc, pro_price, buy_num, shop, address])
    

    完整代码

    from selenium import webdriver
    import time
    import csv
    import re
    # 搜索商品,获取商品页码
    def search_product(key_word):
        # 定位输入框
        browser.find_element_by_id("q").send_keys(key_word)
        # 定义点击按钮,并点击
        browser.find_element_by_class_name('btn-search').click()
        # 最大化窗口:为了方便我们扫码
        browser.maximize_window()
        # 等待15秒,给足时间我们扫码
        time.sleep(15)
        # 定位这个“页码”,获取“共100页这个文本”
        page_info = browser.find_element_by_xpath('//div[@class="total"]').text
        # 需要注意的是:findall()返回的是一个列表,虽然此时只有一个元素它也是一个列表。
        page = re.findall("(d+)",page_info)[0]
        return page
    
    # 获取数据
    def get_data():
        # 通过页面分析发现:所有的信息都在items节点下
        items = browser.find_elements_by_xpath('//div[@class="items"]/div[@class="item J_MouserOnverReq  "]')
        for item in items:
            # 参数信息
            pro_desc = item.find_element_by_xpath('.//div[@class="row row-2 title"]/a').text
            # 价格
            pro_price = item.find_element_by_xpath('.//strong').text
            # 付款人数
            buy_num = item.find_element_by_xpath('.//div[@class="deal-cnt"]').text
            # 旗舰店
            shop = item.find_element_by_xpath('.//div[@class="shop"]/a').text
            # 发货地
            address = item.find_element_by_xpath('.//div[@class="location"]').text
            #print(pro_desc, pro_price, buy_num, shop, address)
            with open('{}.csv'.format(key_word), mode='a', newline='', encoding='utf-8-sig') as f:
                csv_writer = csv.writer(f, delimiter=',')
                csv_writer.writerow([pro_desc, pro_price, buy_num, shop, address])
    
    def main():
        #浏览器要获取的链接
        browser.get('https://www.taobao.com/')
        # 要获取的关键字
        page = search_product(key_word)
        print(page)
        get_data()
        page_num = 1
        while int(page) != page_num:
            print("*" * 100)
            print("正在爬取第{}页".format(page_num + 1))
            browser.get('https://s.taobao.com/search?q={}&s={}'.format(key_word, page_num*44))
            browser.implicitly_wait(15)
            get_data()
            page_num += 1
        print("数据爬取完毕!")
    
    if __name__ == '__main__':
        key_word = input("请输入你要搜索的商品:")
        browser = webdriver.Chrome()
        main()
    
  • 相关阅读:
    括号匹配问题:判断括号式子是否匹配。如{[()]}是匹配的,而{[[])}是不匹配的。
    回文判断的两种方法
    将A链表中的奇数元素与偶数元素拆成两个链表A和B,A中是奇数元素,B是偶数元素。要求拆除后保持链表元素原来的相对位置
    修改kail linux的IP地址等网络信息
    VXDIAG SUBARU SSM III错误许可解决方案
    Mini ACDP更新CAS3数据提示和指南
    thinkphp框架中find()和select()的区别
    PHP中=>和->以及::的用法
    Golang 开发规范(JD )
    JD MySQL数据库开发规范(绝密,企业级开发中Mysql规范)
  • 原文地址:https://www.cnblogs.com/James-221/p/13791945.html
Copyright © 2011-2022 走看看