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 base64编码解码实例
Jun 21 Python
Python中subprocess的简单使用示例
Jul 28 Python
Python实现嵌套列表及字典并按某一元素去重复功能示例
Nov 30 Python
python中numpy的矩阵、多维数组的用法
Feb 05 Python
python使用socket创建tcp服务器和客户端
Apr 12 Python
Python实现正弦信号的时域波形和频谱图示例【基于matplotlib】
May 04 Python
对python中的高效迭代器函数详解
Oct 18 Python
Python读取指定日期邮件的实例
Feb 01 Python
python实现图片中文字分割效果
Jul 22 Python
Python通过两个dataframe用for循环求笛卡尔积
Apr 29 Python
OpenCV中resize函数插值算法的实现过程(五种)
Jun 05 Python
Python的代理类实现,控制访问和修改属性的权限你都了解吗
Mar 21 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
经典的PHPer为什么被认为是草根?
2007/04/02 PHP
有关 PHP 和 MySQL 时区的一点总结
2008/03/26 PHP
php实现jQuery扩展函数
2009/10/30 PHP
PHP并发查询MySQL的实例代码
2017/08/09 PHP
JS提交并解析后台返回的XML的代码
2008/11/03 Javascript
js中onload与onunload的使用示例
2013/08/25 Javascript
JS 有趣的eval优化输入验证实例代码
2013/09/22 Javascript
IE、FF浏览器下修改标签透明度
2014/01/28 Javascript
基于jQuery实现下拉框
2014/11/24 Javascript
JavaScript返回上一页的三种方法及区别介绍
2015/07/04 Javascript
解决wx.onMenuShareTimeline出现的问题
2016/08/16 Javascript
详解angularJS动态生成的页面中ng-click无效解决办法
2017/06/19 Javascript
jQuery实现拖动效果的实例代码
2017/06/25 jQuery
js实现带进度条提示的多视频上传功能
2020/12/13 Javascript
vue中引用阿里字体图标的方法
2018/02/10 Javascript
Node.js 使用axios读写influxDB的方法示例
2018/10/26 Javascript
详解Node.js 中使用 ECDSA 签名遇到的坑
2018/11/26 Javascript
Vue 实例中使用$refs的注意事项
2021/01/29 Vue.js
[41:52]2018DOTA2亚洲邀请赛3月29日小组赛B组Effect VS Secret
2018/03/30 DOTA
Python连接DB2数据库
2016/08/27 Python
Python中的左斜杠、右斜杠(正斜杠和反斜杠)
2016/08/30 Python
使用Python和Prometheus跟踪天气的使用方法
2019/05/06 Python
Python tensorflow实现mnist手写数字识别示例【非卷积与卷积实现】
2019/12/19 Python
Django中ORM找出内容不为空的数据实例
2020/05/20 Python
Python json解析库jsonpath原理及使用示例
2020/11/25 Python
涂鸦板简单实现 Html5编写属于自己的画画板
2016/07/05 HTML / CSS
万宝龙英国官网:Montblanc手表、书写工具、皮革和珠宝
2018/10/16 全球购物
SCDKey德国:全球领先的数字游戏市场
2019/04/09 全球购物
大二自我鉴定范文
2013/10/05 职场文书
市政施工员自我鉴定
2014/01/15 职场文书
数学教学随笔感言
2014/02/17 职场文书
党员创先争优承诺书
2014/03/26 职场文书
关于中国梦的演讲稿
2014/04/23 职场文书
文艺晚会策划方案
2014/06/11 职场文书
综治维稳工作汇报
2014/10/27 职场文书
2015新学期开学寄语
2015/02/26 职场文书