python爬虫爬取淘宝商品信息(selenum+phontomjs)


Posted in Python onFebruary 24, 2018

本文实例为大家分享了python爬虫爬取淘宝商品的具体代码,供大家参考,具体内容如下

1、需求目标

进去淘宝页面,搜索耐克关键词,抓取 商品的标题,链接,价格,城市,旺旺号,付款人数,进去第二层,抓取商品的销售量,款号等。

python爬虫爬取淘宝商品信息(selenum+phontomjs)

python爬虫爬取淘宝商品信息(selenum+phontomjs)

python爬虫爬取淘宝商品信息(selenum+phontomjs)

2、结果展示

python爬虫爬取淘宝商品信息(selenum+phontomjs)

3、源代码

# encoding: utf-8
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import time
import pandas as pd
time1=time.time()
from lxml import etree
from selenium import webdriver
#########自动模拟
driver=webdriver.PhantomJS(executable_path='D:/Python27/Scripts/phantomjs.exe')
import re

#################定义列表存储#############
title=[]
price=[]
city=[]
shop_name=[]
num=[]
link=[]
sale=[]
number=[]

#####输入关键词耐克(这里必须用unicode)
keyword="%E8%80%90%E5%85%8B"


for i in range(0,1):

  try:
    print "...............正在抓取第"+str(i)+"页..........................."

    url="https://s.taobao.com/search?q=%E8%80%90%E5%85%8B&imgfile=&js=1&stats_click=search_radio_all%3A1&initiative_id=staobaoz_20170710&ie=utf8&bcoffset=4&ntoffset=4&p4ppushleft=1%2C48&s="+str(i*44)
    driver.get(url)
    time.sleep(5)
    html=driver.page_source

    selector=etree.HTML(html)
    title1=selector.xpath('//div[@class="row row-2 title"]/a')
    for each in title1:
      print each.xpath('string(.)').strip()
      title.append(each.xpath('string(.)').strip())


    price1=selector.xpath('//div[@class="price g_price g_price-highlight"]/strong/text()')
    for each in price1:
      print each
      price.append(each)


    city1=selector.xpath('//div[@class="location"]/text()')
    for each in city1:
      print each
      city.append(each)


    num1=selector.xpath('//div[@class="deal-cnt"]/text()')
    for each in num1:
      print each
      num.append(each)


    shop_name1=selector.xpath('//div[@class="shop"]/a/span[2]/text()')
    for each in shop_name1:
      print each
      shop_name.append(each)


    link1=selector.xpath('//div[@class="row row-2 title"]/a/@href')
    for each in link1:
      kk="https://" + each


      link.append("https://" + each)
      if "https" in each:
        print each

        driver.get(each)
      else:
        print "https://" + each
        driver.get("https://" + each)
      time.sleep(3)
      html2=driver.page_source
      selector2=etree.HTML(html2)

      sale1=selector2.xpath('//*[@id="J_DetailMeta"]/div[1]/div[1]/div/ul/li[1]/div/span[2]/text()')
      for each in sale1:
        print each
        sale.append(each)

      sale2=selector2.xpath('//strong[@id="J_SellCounter"]/text()')
      for each in sale2:
        print each
        sale.append(each)

      if "tmall" in kk:
        number1 = re.findall('<ul id="J_AttrUL">(.*?)</ul>', html2, re.S)
        for each in number1:
          m = re.findall('>*号: (.*?)</li>', str(each).strip(), re.S)
          if len(m) > 0:
            for each1 in m:
              print each1
              number.append(each1)

          else:
            number.append("NULL")

      if "taobao" in kk:
        number2=re.findall('<ul class="attributes-list">(.*?)</ul>',html2,re.S)
        for each in number2:
          h=re.findall('>*号: (.*?)</li>', str(each).strip(), re.S)
          if len(m) > 0:
            for each2 in h:
              print each2
              number.append(each2)

          else:
            number.append("NULL")

      if "click" in kk:
        number.append("NULL")

  except:
    pass


print len(title),len(city),len(price),len(num),len(shop_name),len(link),len(sale),len(number)

# #
# ######数据框
data1=pd.DataFrame({"标题":title,"价格":price,"旺旺":shop_name,"城市":city,"付款人数":num,"链接":link,"销量":sale,"款号":number})
print data1
# 写出excel
writer = pd.ExcelWriter(r'C:\\taobao_spider2.xlsx', engine='xlsxwriter', options={'strings_to_urls': False})
data1.to_excel(writer, index=False)
writer.close()

time2 = time.time()
print u'ok,爬虫结束!'
print u'总共耗时:' + str(time2 - time1) + 's'
####关闭浏览器
driver.close()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python类继承与子类实例初始化用法分析
Apr 17 Python
使用Python的Tornado框架实现一个一对一聊天的程序
Apr 25 Python
详细解析Python中的变量的数据类型
May 13 Python
在Lighttpd服务器中运行Django应用的方法
Jul 22 Python
详解python运行三种方式
May 13 Python
python bluetooth蓝牙信息获取蓝牙设备类型的方法
Nov 29 Python
TensorFlow实现保存训练模型为pd文件并恢复
Feb 06 Python
使用keras实现densenet和Xception的模型融合
May 23 Python
Python Socket TCP双端聊天功能实现过程详解
Jun 15 Python
Python numpy矩阵处理运算工具用法汇总
Jul 13 Python
Python操作PostgreSql数据库的方法(基本的增删改查)
Dec 29 Python
python编程学习使用管道Pipe编写优化代码
Nov 20 Python
python正则表达式爬取猫眼电影top100
Feb 24 #Python
python爬虫获取淘宝天猫商品详细参数
Jun 23 #Python
python按综合、销量排序抓取100页的淘宝商品列表信息
Feb 24 #Python
python2.7+selenium2实现淘宝滑块自动认证功能
Feb 24 #Python
Python 中Pickle库的使用详解
Feb 24 #Python
Python使用Selenium+BeautifulSoup爬取淘宝搜索页
Feb 24 #Python
python3+mysql查询数据并通过邮件群发excel附件
Feb 24 #Python
You might like
如何给phpcms v9增加类似于phpcms 2008中的关键词表
2013/07/01 PHP
php使用pear_smtp发送邮件
2016/04/15 PHP
jMessageBox 基于jQuery的窗口插件
2009/12/09 Javascript
7个JS基础知识总结
2014/03/05 Javascript
SyntaxHighlighter 3.0.83使用笔记
2015/01/26 Javascript
localResizeIMG先压缩后使用ajax无刷新上传(移动端)
2015/08/11 Javascript
原生JavaScript实现滚动条效果
2020/03/24 Javascript
Bootstrap 粘页脚效果
2016/03/28 Javascript
Html5 js实现手风琴效果
2020/04/17 Javascript
touch.js 拖动、缩放、旋转 (鼠标手势)功能代码
2017/02/04 Javascript
jQuery控制元素隐藏和显示
2017/03/03 Javascript
ES6数组的扩展详解
2017/04/25 Javascript
vue-cli脚手架搭建的项目去除eslint验证的方法
2018/09/29 Javascript
深入理解JavaScript的值传递和引用传递
2018/10/24 Javascript
微信小程序版本自动更新的方法
2019/06/14 Javascript
[02:34]DOTA2亚洲邀请赛 BG战队出场宣传片
2015/03/09 DOTA
[01:30:55]VG vs Mineski Supermajor 败者组 BO3 第三场 6.6
2018/06/07 DOTA
详解Django框架中用户的登录和退出的实现
2015/07/23 Python
使用Python的Flask框架构建大型Web应用程序的结构示例
2016/06/04 Python
Python 专题六 局部变量、全局变量global、导入模块变量
2017/03/20 Python
深入理解Python3 内置函数大全
2017/11/23 Python
Python scipy的二维图像卷积运算与图像模糊处理操作示例
2019/09/06 Python
最新pycharm安装教程
2020/11/18 Python
python如何发送带有附件、正文为HTML的邮件
2021/02/27 Python
最热门的自我评价
2013/12/30 职场文书
预备党员转正思想汇报
2014/01/12 职场文书
生日礼品店创业计划书范文
2014/03/21 职场文书
酒店开业策划方案
2014/06/02 职场文书
中学生社会实践活动总结
2014/07/03 职场文书
党的群众路线教育实践活动领导班子对照检查材料
2014/09/25 职场文书
教师党员自我评价范文
2015/03/04 职场文书
妇联2016年六一国际儿童节活动总结
2016/04/06 职场文书
古诗之爱国古诗5首
2019/09/20 职场文书
python实现A*寻路算法
2021/06/13 Python
世界无敌的ICOM IC-R9500宽频接收机
2022/03/25 无线电
使用refresh_token实现无感刷新页面
2022/04/26 Javascript