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 29 Python
python指定写入文件时的编码格式方法
Jun 07 Python
Python3实现将本地JSON大数据文件写入MySQL数据库的方法
Jun 13 Python
TensorFlow打印tensor值的实现方法
Jul 27 Python
python实现指定字符串补全空格、前面填充0的方法
Nov 16 Python
Pandas库之DataFrame使用的学习笔记
Jun 21 Python
Django时区详解
Jul 24 Python
python制作朋友圈九宫格图片
Nov 03 Python
Python中类似于jquery的pyquery库用法分析
Dec 02 Python
python deque模块简单使用代码实例
Mar 12 Python
python和php哪个更适合写爬虫
Jun 22 Python
在pytorch中动态调整优化器的学习率方式
Jun 24 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 移除数组重复元素的一点说明
2008/11/27 PHP
PHP开发中csrf攻击的简单演示和防范
2017/05/07 PHP
php实现和c#一致的DES加密解密实例
2017/07/24 PHP
Laravel框架模板继承操作示例
2018/06/11 PHP
PHP中有关长整数的一些操作教程
2019/09/11 PHP
laravel框架中表单请求类型和CSRF防护实例分析
2019/11/23 PHP
php回调函数处理数组操作示例
2020/04/13 PHP
Aster vs Newbee BO5 第一场2.19
2021/03/10 DOTA
js自动下载文件到本地的实现代码
2013/04/28 Javascript
利用js实现遮罩以及弹出可移动登录窗口
2013/07/08 Javascript
jquery等宽输出文字插件使用介绍
2013/09/18 Javascript
JavaScript实现生成GUID(全局统一标识符)
2014/09/05 Javascript
jquery实现点击展开列表同时隐藏其他列表
2015/08/10 Javascript
直接拿来用的15个jQuery代码片段
2015/09/23 Javascript
html判断当前页面是否在iframe中的实例
2016/11/30 Javascript
利用vue实现模态框组件
2016/12/19 Javascript
Vue.js学习之计算属性
2017/01/22 Javascript
微信小程序实战之上拉(分页加载)效果(2)
2017/04/17 Javascript
Vue路由history模式解决404问题的几种方法
2018/09/29 Javascript
js实现鼠标拖拽div左右滑动
2020/01/15 Javascript
python远程登录代码
2008/04/29 Python
python模拟登录并且保持cookie的方法详解
2017/04/04 Python
python构建深度神经网络(DNN)
2018/03/10 Python
python Celery定时任务的示例
2018/03/13 Python
解决已经安装requests,却依然提示No module named requests问题
2018/05/18 Python
Python 字符串与数字输出方法
2018/07/16 Python
python将print输出的信息保留到日志文件中
2019/09/27 Python
解决Django部署设置Debug=False时xadmin后台管理系统样式丢失
2020/04/07 Python
Django自定义YamlField实现过程解析
2020/11/11 Python
localstorage和sessionstorage使用记录(推荐)
2017/05/23 HTML / CSS
工作表扬信的范文
2014/01/10 职场文书
5.1手机促销活动
2014/01/17 职场文书
秘书英文求职信范文
2014/01/31 职场文书
我的祖国演讲稿
2014/05/04 职场文书
幽默导游词应该怎么写?
2019/08/26 职场文书
MySQL一劳永逸永久支持输入中文的方法实例
2022/08/05 MySQL