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实现rest请求api示例
Apr 22 Python
python中 chr unichr ord函数的实例详解
Aug 06 Python
Python扩展内置类型详解
Mar 26 Python
使用Python如何测试InnoDB与MyISAM的读写性能
Sep 18 Python
python抓取网页内容并进行语音播报的方法
Dec 24 Python
浅谈python str.format与制表符\t关于中文对齐的细节问题
Jan 14 Python
python 读写excel文件操作示例【附源码下载】
Jun 19 Python
django如何自己创建一个中间件
Jul 24 Python
python实现猜拳小游戏
Apr 05 Python
Python如何使用Gitlab API实现批量的合并分支
Nov 27 Python
Python识别验证码的实现示例
Sep 30 Python
教你使用pyinstaller打包Python教程
May 27 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实现浏览器点击下载TXT文档的方法详解
2013/06/02 PHP
PHP中执行cmd命令的方法
2014/10/11 PHP
thinkphp3.2.3版本的数据库增删改查实现代码
2016/09/22 PHP
兼容Mozilla必须知道的知识。
2007/01/09 Javascript
在jQuery中 关于json空对象筛选替换
2013/04/15 Javascript
将list转换为json失败的原因
2013/12/17 Javascript
Javascript基础教程之数组 array
2015/01/18 Javascript
javascript 实现 原路返回
2015/01/21 Javascript
JavaScript随机生成信用卡卡号的方法
2015/04/07 Javascript
JQuery中属性过滤选择器用法实例分析
2015/05/18 Javascript
JavaScript实现跨浏览器的添加及删除事件绑定函数实例
2015/08/04 Javascript
Node.js与Sails redis组件的使用教程
2017/02/14 Javascript
Vue2.0父子组件传递函数的教程详解
2017/10/16 Javascript
js插件实现图片滑动验证码
2020/09/29 Javascript
vue父组件向子组件传递多个数据的实例
2018/03/01 Javascript
vue.js 使用axios实现下载功能的示例
2018/03/05 Javascript
vue发送ajax请求详解
2018/10/09 Javascript
使用Vue 自定义文件选择器组件的实例代码
2020/03/04 Javascript
jQuery实现简单QQ聊天框
2020/08/27 jQuery
[03:43]2014DOTA2西雅图国际邀请赛 newbee战队巡礼
2014/07/07 DOTA
python通过urllib2获取带有中文参数url内容的方法
2015/03/13 Python
Python中使用logging模块打印log日志详解
2015/04/05 Python
关于Numpy中的行向量和列向量详解
2019/11/30 Python
Python注释、分支结构、循环结构、伪“选择结构”用法实例分析
2020/01/09 Python
Python 实现判断图片格式并转换,将转换的图像存到生成的文件夹中
2020/01/13 Python
Python3 利用face_recognition实现人脸识别的方法
2020/03/13 Python
python 判断一组数据是否符合正态分布
2020/09/23 Python
Python 打印自己设计的字体的实例讲解
2021/01/04 Python
Topman美国官网:英国著名的国际平价时尚男装品牌
2017/12/22 全球购物
美国健康和保健平台:healtop
2020/07/02 全球购物
巴西本土电商平台:Americanas
2020/06/21 全球购物
临床医师个人自我评价
2014/04/06 职场文书
应急处置方案
2014/06/16 职场文书
工作业绩不及格检讨书
2014/10/28 职场文书
30岁前绝不能错过的10本书
2019/08/08 职场文书
Nginx报404错误的详细解决方法
2022/07/23 Servers