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 09 Python
Python 序列的方法总结
Oct 18 Python
python list排序的两种方法及实例讲解
Mar 20 Python
Python基于Pymssql模块实现连接SQL Server数据库的方法详解
Jul 20 Python
python列表list保留顺序去重的实例
Dec 14 Python
Python实现KNN(K-近邻)算法的示例代码
Mar 05 Python
Django文件上传与下载(FileFlid)
Oct 06 Python
python十进制转二进制的详解
Feb 07 Python
Django form表单与请求的生命周期步骤详解
Jun 07 Python
详解向scrapy中的spider传递参数的几种方法(2种)
Sep 28 Python
python3排序的实例方法
Oct 20 Python
python数据分析之用sklearn预测糖尿病
Apr 22 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
php学习之数据类型之间的转换介绍
2011/06/09 PHP
PHP设计模式之迭代器模式的深入解析
2013/06/13 PHP
php按单词截取字符串的方法
2015/04/07 PHP
Yii2中DropDownList简单用法示例
2016/07/18 PHP
PHP Mysqli 常用代码集合
2016/11/12 PHP
PHP如何通过带尾指针的链表实现'队列'
2020/10/22 PHP
jQuery 跨域访问问题解决方法
2009/12/02 Javascript
js实现支持手机滑动切换的轮播图片效果实例
2015/04/29 Javascript
jquery+json实现动态商品内容展示的方法
2016/01/14 Javascript
jQuery实现的精美平滑二级下拉菜单效果代码
2016/03/28 Javascript
JQuery之proxy实现绑定代理方法
2016/08/01 Javascript
将json转换成struts参数的方法
2016/11/08 Javascript
谈谈对vue响应式数据更新的误解
2017/08/01 Javascript
nodejs使用express获取get和post传值及session验证的方法
2017/11/09 NodeJs
详解vue的diff算法原理
2018/05/20 Javascript
JavaScript设计模式之享元模式实例详解
2019/01/17 Javascript
详解几十行代码实现一个vue的状态管理
2019/01/28 Javascript
JS实现的贪吃蛇游戏案例详解
2019/05/01 Javascript
JS实现图片懒加载(lazyload)过程详解
2020/04/02 Javascript
ES6函数实现排它两种写法解析
2020/05/13 Javascript
详解datagrid使用方法(重要)
2020/11/06 Javascript
python实现带错误处理功能的远程文件读取方法
2015/04/29 Python
简单的python后台管理程序
2017/04/13 Python
python编程使用selenium模拟登陆淘宝实例代码
2018/01/25 Python
python实现俄罗斯方块
2018/06/26 Python
PyTorch 1.0 正式版已经发布了
2018/12/13 Python
python基于paramiko将文件上传到服务器代码实现
2019/07/08 Python
python线程join方法原理解析
2020/02/11 Python
残疾人创业典型事迹
2014/02/01 职场文书
电子商务专业学生职业生涯规划
2014/03/07 职场文书
法定代表人授权委托书范本
2014/10/07 职场文书
法学专业求职信范文
2015/03/19 职场文书
校运会班级霸气口号
2015/12/24 职场文书
当你焦虑迷茫时,请读读这6句话
2019/07/24 职场文书
两行代码解决Jupyter Notebook中文不能显示的问题
2021/04/24 Python
自动在Windows中运行Python脚本并定时触发功能实现
2021/09/04 Python