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 爬虫出现403禁止访问错误详解

python 爬虫解决403禁止访问错误 在Python写爬虫的时候,html.getcode()会遇到403禁止访问的问题,这是网站对自动化爬虫的禁止,要解决这个问题,需要用到pyth...

python模拟新浪微博登陆功能(新浪微博爬虫)

1、主函数(WeiboMain.py): 复制代码 代码如下:import urllib2import cookielib import WeiboEncodeimport WeiboS...

Python 正则表达式爬虫使用案例解析

现在拥有了正则表达式这把神兵利器,我们就可以进行对爬取到的全部网页源代码进行筛选了。 下面我们一起尝试一下爬取内涵段子网站: http://www.neihan8.com/articl...

Pyspider中给爬虫伪造随机请求头的实例

Pyspider 中采用了 tornado 库来做 http 请求,在请求过程中可以添加各种参数,例如请求链接超时时间,请求传输数据超时时间,请求头等等,但是根据pyspider的原始框...

python urllib爬取百度云连接的实例代码

python urllib爬取百度云连接的实例代码

翻看自己以前写的程序,发现写过一个爬取盘多多百度云资源的东西,完全是当时想看变形金刚才自己写的,而且当时第一次接触python大概写了有2天才搞出来这个程序,学习python语言,可以看...