【需求】输入关键字,如书包,可以搜索出对应商品的信息,包括:商品标题、商品链接、价格范围;且最终的商品信息需要符合:包邮、价格差不会超过某数值

#coding=utf-8
"""
以下三个字可以自行设置:search_keyword、page、price_interval_max
"""
#设置搜索的关键字
search_keyword = "戒指"
#设置需要搜索的商品的页数,比如设置10,就是淘宝搜出结果中前10页的商品数据,淘宝默认一页有44个商品
page = 10
#设置最大价格和最小价格之间可接受的差
price_interval_max = 1000 import re, os, requests, sys, time, shutil
from selenium import webdriver
from lxml import etree
from xlrd import open_workbook
from xlutils.copy import copy
reload(sys)
sys.setdefaultencoding( "utf-8" ) time1 = time.time()
phantomjs_path = os.getcwd() + "phantomjs.exe"
driver=webdriver.PhantomJS(executable_path='D:/Python27/Scripts/phantomjs.exe')
# driver=webdriver.PhantomJS(executable_path=phantomjs_path)
search_url = 'https://s.taobao.com/search'
payload = {'q':search_keyword, 's':'', 'ie':'utf8'} #字典传递url参数
payload1 = {'ie':'utf8'}
excel_path_ori = os.getcwd() + "//result.xls"
excel_path = os.getcwd() + "//tb_result.xls"
if not os.path.exists(excel_path):
shutil.copy(excel_path_ori, excel_path)
else:
os.remove(excel_path)
shutil.copy(excel_path_ori, excel_path)
file = open('taobao_test.txt', 'w') sheetName = "Sheet1"
url_lineindex = 0
title_lineindex = 1
price_lineindex = 2
price_interval_lineindex = 3
interval_lineindex = 4
fee_lineindex = 5 def Write_Excel(rowIndex, lineIndex, content):
"""
- rowIndex:行
- lineIndex:列
"""
rowIndex = int(rowIndex)
lineIndex = int(lineIndex)
rb = 'r+w'
rb = open_workbook(excel_path, 'r')
rbook = open_workbook(excel_path, 'w')
wb = copy(rbook)
sheetIndex = rbook.sheet_names().index(sheetName)
wb.get_sheet(int(sheetIndex)).write(int(rowIndex), int(lineIndex), content)
wb.save(excel_path) def get_detail_price(url):
"""
获取价格范围字段
:param url:
:return:
"""
driver.get(url)
time.sleep(1)
html=driver.page_source
selector=etree.HTML(html)
if "tmall" in url:
detail_price = selector.xpath('//div[@class="tm-promo-price"]/span[@class="tm-price"]/text()') elif "taobao" in url:
detail_price = selector.xpath('//em[@class="tb-rmb-num"]/text()')
return detail_price def get_price_interval(price):
"""
部分商品的价格是一个范围,如:12.00-25.00,以下获取价格范围,及价格差
:param price:
:return:
"""
print price
price_interval = price[0]
price_interval = ''.join(price_interval)
if "-" in price_interval:
start_price = price_interval.split("-")[0]
end_price = price_interval.split("-")[1]
interval = float(end_price) - float(start_price)
else:
interval = 0
return price_interval, interval def get_url_test():
"""
获取商品信息:标题、链接、最大价格、价格范围、价格差
:return:NONE
"""
j = 0
Write_Excel(j, url_lineindex, u"商品链接")
Write_Excel(j, title_lineindex, u"商品标题")
Write_Excel(j, price_lineindex, u"最低价格")
Write_Excel(j, price_interval_lineindex, u"价格范围")
Write_Excel(j, interval_lineindex, u"价格差")
Write_Excel(j, fee_lineindex, u"运费")
for k in range(0, page): #10次,就是10页的商品数据 payload['s'] = 44 * k + 1 #此处改变的url参数为s,s为1时第一页,s为45是第二页,89时第三页以此类推
resp = requests.get(search_url, params=payload)
#设置编码
title = re.findall(r'"raw_title":"([^"]+)"', resp.text, re.I) #正则保存所有raw_title的内容,这个是书名,下面是价格,地址
price = re.findall(r'"view_price":"([^"]+)"', resp.text, re.I)
loc = re.findall(r'"i003d568963194127tem_loc":"([^"]+)"', resp.text, re.I)
url = re.findall(r'"detail_url":"([^"]+)"', resp.text, re.I)
fee = re.findall(r'"view_fee":"([^"]+)"', resp.text, re.I)
x = len(title) #每一页商品的数量 for i in range(0, x) : #把缓冲中的数据保存到文件中
print i
print('商品标题:' + title[i])
print('最低价格:' + price[i])
print('运费:' + fee[i])
#获取商品链接
url[i] = url[i].replace("\u003d","=").replace("\u0026","&")
# print('goods_url:' + url[i])
url[i] = "https:" + url[i]
print('商品链接:' + url[i])
#获取商品价格区间
try:
resp_detail = requests.get(url[i])
resp_detail.encoding = 'utf-8'
detail_price = get_detail_price(url[i])
data = get_price_interval(detail_price)
price_interval = data[0]
interval = data[1]
print('price_interval:' + price_interval)
print('interval:' + str(interval))
#保存数据
file.write(
str(k * 44 + i + 1) +
'商品链接:' + url[i] + '\n' +
'商品标题:' + title[i] + '\n' +
'最低价格:' + price[i] + '\n' +
'价格范围:' + str(price_interval) + '\n' +
'价格差:' + str(interval) + '\n' )
# 'goods_fee:' + fee[i] + '\n')
#将过滤数据写入excel表格
if fee[i] == "0.00" and interval < int(price_interval_max):
print "该商品符合要求:包邮,且最大价格与最小价格差小于%s" % price_interval_max
j = j + 1
Write_Excel(j, url_lineindex, url[i])
Write_Excel(j, title_lineindex, title[i])
Write_Excel(j, price_lineindex, price[i])
Write_Excel(j, price_interval_lineindex, price_interval)
Write_Excel(j, interval_lineindex, interval)
Write_Excel(j, fee_lineindex, fee[i])
except:
print "该商品信息获取失败,跳过"
continue get_url_test()
# #环境恢复
file.close()
os.system("taskkill /im phantomjs.exe")
time2 = time.time()
print u'ok,结束!'
print u'总共耗时:' + str((time2 - time1)/60) + '分钟'

python:爬虫获取淘宝/天猫的商品信息的更多相关文章

  1. Python爬虫 获得淘宝商品评论

    自从写了第一个sina爬虫,便一发不可收拾.进入淘宝评论爬虫正题: 在做这个的时候,也没有深思到底爬取商品评论有什么用,后来,爬下来了数据.觉得这些数据可以用于帮助分析商品的评论,从而为用户选择商品提 ...

  2. Python 爬虫知识点 - 淘宝商品检索结果抓包分析(续一)

    通过前一节得出地址可能的构建规律,如下: https://s.taobao.com/search?data-key=s&data-value=44&ajax=true&_ksT ...

  3. Python 爬虫知识点 - 淘宝商品检索结果抓包分析

    一.抓包基础 在淘宝上搜索“Python机器学习”之后,试图抓取书名.作者.图片.价格.地址.出版社.书店等信息,查看源码发现html-body中没有这些信息,分析脚本发现,数据存储在了g_page_ ...

  4. python爬虫——用selenium爬取京东商品信息

    1.先附上效果图(我偷懒只爬了4页)  2.京东的网址https://www.jd.com/ 3.我这里是不加载图片,加快爬取速度,也可以用Headless无弹窗模式 options = webdri ...

  5. Python 爬虫知识点 - 淘宝商品检索结果抓包分析(续二)

    一.URL分析 通过对“Python机器学习”结果抓包分析,有两个无规律的参数:_ksTS和callback.通过构建如下URL可以获得目标关键词的检索结果,如下所示: https://s.taoba ...

  6. [PHP] 编写爬虫获取淘宝网上所有的商品分类以及关键属性 销售属性 非关键属性数据

    参考文章地址:https://blog.csdn.net/zhengzizhi/article/details/80716608 http://open.taobao.com/apitools/api ...

  7. 简单的抓取淘宝关键字信息、图片的Python爬虫|Python3中级玩家:淘宝天猫商品搜索爬虫自动化工具(第一篇)

    Python3中级玩家:淘宝天猫商品搜索爬虫自动化工具(第一篇) 淘宝改字段,Bugfix,查看https://github.com/hunterhug/taobaoscrapy.git 由于Gith ...

  8. Python网页信息采集:使用PhantomJS采集淘宝天猫商品内容

    1,引言 最近一直在看Scrapy 爬虫框架,并尝试使用Scrapy框架写一个可以实现网页信息采集的简单的小程序.尝试过程中遇到了很多小问题,希望大家多多指教. 本文主要介绍如何使用Scrapy结合P ...

  9. python 获取淘宝商品信息

    python cookie 获取淘宝商品信息 # //get_goods_from_taobao import requests import re import xlsxwriter cok='' ...

随机推荐

  1. oracle中next_day()、last_day()函数解析

    oracle中next_day()函数解析 Sql代码 当前系统时间的下一星期一的时间select   next_day(sysdate,1) from dual NEXT_DAY(date,char ...

  2. ASP.NET中出现内存溢出错误System.OutOfMemoryException

    原因1:数据库服务器性能问题导致内存不够用,从而引起内存溢出 原因2:在IIS的应用程序池中进行配置,引起IIS服务器的内存分配问题,从而引起内存溢出   分析:      32位操作系统的寻址空间是 ...

  3. APPlication,Session和Cookie的区别

    方法 信息量大小 保存时间 应用范围 保存位置 Application 任意大小 整个应用程序的生命期 所有用户 服务器端 Session 小量,简单的数据 用户活动时间+一段延迟时间(一般为20分钟 ...

  4. 如何使用Nunit进行测试(Visual Studio 2017 comminity)

    一.环境 操作系统:Windows 版本 10.0.15063 64位 集成环境:Visual Studio 2017 comminity(此后简称vs2017) 编程语言:C# 目标框架:.NET ...

  5. Python3之set, frozenset记录

    set1 = set([1, 2, 3, 4]) set2 = frozenset([1, 2, 3, 4]) print(set1, set2, sep='|||') set1.add(" ...

  6. 【转载】Ocelot网关的路由热更新

    调用API修改Ocelot的配置文件 May 11, 2018 | netcoreocelot | 410 阅读 Ocelot是一个基于.net core的开源webapi服务网关开源项目,功能比较强 ...

  7. day 12

    一,什么是装饰器? 装饰器本质上就是一个python函数,他可以让其他函数在不需要做任何代码变动的前提下,增加额外的功能,装饰器的返回值也是一个函数对象. 装饰器的应用场景:比如插入日志,性能测试,事 ...

  8. mysql: 查看某库表大小

    查询所有数据库占用磁盘空间大小的SQL语句: 语句如下 1 2 3 4 5 select TABLE_SCHEMA, concat(truncate(sum(data_length)/1024/102 ...

  9. 2、php中字符串单引号好和双引号的区别

    使用单引号和双引号的主要区别是:单引号定义的字符串中出现的变量和转义序列不会被变量的值代替,而双引号中使用变量名会显示该变量的值.

  10. C#.NET XML报文签名与验签

    -- MD5Util: using System; using System.Collections.Generic; using System.Security.Cryptography; usin ...