Python进阶之使用selenium爬取淘宝商品信息功能示例

yipeiwu_com6年前Python爬虫

本文实例讲述了Python进阶之使用selenium爬取淘宝商品信息功能。分享给大家供大家参考,具体如下:

# encoding=utf-8
__author__ = 'Jonny'
__location__ = '西安'
__date__ = '2018-05-14'
'''
需要的基本开发库文件:
requests,pymongo,pyquery,selenium
开发流程:
  搜索关键字:利用selenium驱动浏览器搜索关键字,得到查询后的商品列表
  分析页码并翻页:得到商品页码数,模拟翻页,得到后续页面的商品列表
  分析提取商品内容:利用PyQuery分析页面源代码,解析获得商品列表信息
  存储到MongDB中:将商品的信息列表存储到数据库MongoDB。
'''
import requests
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
import pymongo
import re
import time
browser = webdriver.Chrome()
wait = WebDriverWait(browser,10)
client = pymongo.MongoClient('localhost',27017)
mongo = client['taobao']
def searcher():
  url = 'https://www.taobao.com/'
  browser.get(url=url)
  try:
    #判断页面加载是够成功,设置等待时间
    #判断位置1:搜索输入框是否加载完成
    input_kw = wait.until(
      EC.presence_of_element_located((By.CSS_SELECTOR, '#q'))
    )
    #判断位置2:搜索输入框对应的搜索按键是否加载完成
    submit = wait.until(EC.element_to_be_clickable(
      (By.CSS_SELECTOR,'#J_TSearchForm > div.search-button > button'))
    )
    input_kw.send_keys('男装')
    submit.click()
    #等待页面加载完成,便于准确判断网页的总页数
    page_counts = wait.until(
      EC.presence_of_element_located(
        (By.CSS_SELECTOR,'#mainsrp-pager > div > div > div > div.total'))
    )
    parse_page()
    return page_counts.text
  except TimeoutException as e:
    print(e.args)
    return searcher()
#实现翻页
def next_page(page_number):
  try:
    # 判断页面加载是够成功,设置等待时间
    # 判断位置1:页面跳转输入页是否加载完成
    input_page = wait.until(
      EC.presence_of_element_located((By.CSS_SELECTOR, '#mainsrp-pager > div > div > div > div.form > input'))
    )
    # 判断位置2:确认按键是否加载完成
    submit = wait.until(EC.element_to_be_clickable(
      (By.CSS_SELECTOR, '#mainsrp-pager > div > div > div > div.form > span.btn.J_Submit'))
    )
    input_page.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'),str(page_number))
    )
    parse_page()
  except TimeoutException as e:
    print(e.args)
    next_page(page_number)
#对页面进行数据处理
def parse_page():
  # wait.until(EC.presence_of_element_located(By.CSS_SELECTOR,'#mainsrp-itemlist > div > div'))
  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()
  for item in items:
    goods = {
      '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(goods)
    data_storage(goods)
#将数据存入数据库
def data_storage(goods):
  try:
    if mongo['mongo_sheet'].insert(goods):
      print('Successfully storage!')
  except Exception as e:
    print('failedly storage!',goods)
def main():
  text = searcher()
  print(text)
  #获取总页数
  pages = int(re.compile('(\d+)').search(text).group(0))
  print(pages)
  for i in range(2,pages+1):
    next_page(i)
  browser.close()
if __name__ == '__main__':
  main()

更多关于Python相关内容可查看本站专题:《Python Socket编程技巧总结》、《Python正则表达式用法总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总

希望本文所述对大家Python程序设计有所帮助。

相关文章

详解Python爬取并下载《电影天堂》3千多部电影

详解Python爬取并下载《电影天堂》3千多部电影

不知不觉,玩爬虫玩了一个多月了。 我愈发觉得,爬虫其实并不是什么特别高深的技术,它的价值不在于你使用了什么特别牛的框架,用了多么了不起的技术,它不需要。它只是以一种自动化搜集数据的小工具...

python正则爬取某段子网站前20页段子(request库)过程解析

python正则爬取某段子网站前20页段子(request库)过程解析

首先还是谷歌浏览器抓包对该网站数据进行分析,结果如下: 该网站地址:http://www.budejie.com/text 该网站数据都是通过html页面进行展示,网站url默认为第...

Python天气预报采集器实现代码(网页爬虫)

爬虫简单说来包括两个步骤:获得网页文本、过滤得到数据。   1、获得html文本。   python在获取html方面十分方便,寥寥数行代码就可以实现我们需要的功能。 复制代码 代码如下...

python爬虫实现中英翻译词典

本文实例为大家分享了python爬虫实现中英翻译词典的具体代码,供大家参考,具体内容如下 通过根据某平台的翻译资源,提取出翻译信息,并展示出来,包括输入,翻译,输出三个过程,主要利用py...

Python 爬虫学习笔记之单线程爬虫

Python 爬虫学习笔记之单线程爬虫

介绍 本篇文章主要介绍如何爬取麦子学院的课程信息(本爬虫仍是单线程爬虫),在开始介绍之前,先来看看结果示意图 怎么样,是不是已经跃跃欲试了?首先让我们打开麦子学院的网址,然后找到麦子学...