使用Selenium抓取淘宝商品美食信息

一、介绍

还是崔庆才老师的视频,废话不多说,直接上菜

二、流程

  • 目标站点分析
    用浏览器打开淘宝首页输入‘美食’,打开审查元素,分析我们要的商品信息都在Element里哪个分段

  • 搜索关键字
    利用Selenium驱动浏览器搜索关键字,得到查询后的商品列表

  • 分析页码并翻页
    得到商品页码数,模拟翻页,得到后续页面的商品列表

1.png
  • 分析提取商品内容
    利用PyQuery分析源码,解析得到商品列表
2.png
3.png
  • 储存到MongoDB
    将商品列表信息储存到数据库MongoDB

三、代码

#config.py
MONGO_URL='localhost'
MONGO_DB='taobao'
MONGO_TABLE='product'

KEYWORD = '美食'
import re
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from pyquery import PyQuery as pq
from day27.config import *
import pymongo

client=pymongo.MongoClient(MONGO_URL)
db=client[MONGO_DB]
browser=webdriver.Chrome()
wait=WebDriverWait(browser, 10)

def search():
    print('正在搜索')
    try:
        browser.get('http://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('KEYWORD')
        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 next_page(page_number):
    print('正在翻页', 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)

def get_products():
    wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'#mainsrp-itemlist .items .item'))) #使用CSS选择器时,每层要有空格分开
    html=browser.page_source
    doc=pq(html)
    items=doc('#mainsrp-itemlist .items .item').items()  #使用CSS选择器时,每层要有空格分开
    for item in items:
        product={
            'image':item.find('.pic.img').attr('src'),
            'price':item.find('.price').text(),
            'deal':item.find('.deal-cnt').text()[:-3],
            'title':item.find('.title').text(),
            'shop':item.find('.shop').text(),
            'location': item.find('.location').text()
        }
        print(product)
        save_to_mongo(product)

def save_to_mongo(result):
    try:
        if db[MONGO_TABLE].insert(result):
            print('储存到MongoDB成功',result)
    except Exception:
        print('储存到MongoDB失败',result)

def main():
    try:
        total=search()
        total=int(re.compile('(\d+)').search(total).group(1))
        #print(total)
        for i in range(2,total+1):
            next_page(i)
    except Exception:
        print('出错啦')
    finally:
        browser.close()

if __name__=='__main__':
    main()

四、最后得到的数据视图

4.png

五、总结

1.目标站点分析时候访问的url务必和代码里的url一致
2.使用CSS选择器时,每层要有空格分开
3.Compare是个好东西

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 本文是我接触爬虫以来,第三套爬虫的代码记录博客。本文主要是记录淘宝搜索美食的页面信息,工具是selenium 和 ...
    小白猿阅读 4,736评论 1 9
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,058评论 19 139
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,562评论 4 61
  • 爷爷去世快有三年了。 三年前,我还在北京。或许世上真有感应这回事, 那天,我跟EX吵得不可开交,为了一些琐碎的事情...
    丢了朵朵阅读 3,696评论 1 2
  • 非公医疗项目开始进行70% 《穷查理宝典》72% 把三节课最后几堂课程补完 非公医疗项目开始进行50%>70% 《...
    伽蓝214阅读 1,256评论 0 0

友情链接更多精彩内容