python:爬虫获取淘宝/天猫的商品信息
【需求】输入关键字,如书包,可以搜索出对应商品的信息,包括:商品标题、商品链接、价格范围;且最终的商品信息需要符合:包邮、价格差不会超过某数值
#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:爬虫获取淘宝/天猫的商品信息的更多相关文章
- Python爬虫 获得淘宝商品评论
自从写了第一个sina爬虫,便一发不可收拾.进入淘宝评论爬虫正题: 在做这个的时候,也没有深思到底爬取商品评论有什么用,后来,爬下来了数据.觉得这些数据可以用于帮助分析商品的评论,从而为用户选择商品提 ...
- Python 爬虫知识点 - 淘宝商品检索结果抓包分析(续一)
通过前一节得出地址可能的构建规律,如下: https://s.taobao.com/search?data-key=s&data-value=44&ajax=true&_ksT ...
- Python 爬虫知识点 - 淘宝商品检索结果抓包分析
一.抓包基础 在淘宝上搜索“Python机器学习”之后,试图抓取书名.作者.图片.价格.地址.出版社.书店等信息,查看源码发现html-body中没有这些信息,分析脚本发现,数据存储在了g_page_ ...
- python爬虫——用selenium爬取京东商品信息
1.先附上效果图(我偷懒只爬了4页) 2.京东的网址https://www.jd.com/ 3.我这里是不加载图片,加快爬取速度,也可以用Headless无弹窗模式 options = webdri ...
- Python 爬虫知识点 - 淘宝商品检索结果抓包分析(续二)
一.URL分析 通过对“Python机器学习”结果抓包分析,有两个无规律的参数:_ksTS和callback.通过构建如下URL可以获得目标关键词的检索结果,如下所示: https://s.taoba ...
- [PHP] 编写爬虫获取淘宝网上所有的商品分类以及关键属性 销售属性 非关键属性数据
参考文章地址:https://blog.csdn.net/zhengzizhi/article/details/80716608 http://open.taobao.com/apitools/api ...
- 简单的抓取淘宝关键字信息、图片的Python爬虫|Python3中级玩家:淘宝天猫商品搜索爬虫自动化工具(第一篇)
Python3中级玩家:淘宝天猫商品搜索爬虫自动化工具(第一篇) 淘宝改字段,Bugfix,查看https://github.com/hunterhug/taobaoscrapy.git 由于Gith ...
- Python网页信息采集:使用PhantomJS采集淘宝天猫商品内容
1,引言 最近一直在看Scrapy 爬虫框架,并尝试使用Scrapy框架写一个可以实现网页信息采集的简单的小程序.尝试过程中遇到了很多小问题,希望大家多多指教. 本文主要介绍如何使用Scrapy结合P ...
- python 获取淘宝商品信息
python cookie 获取淘宝商品信息 # //get_goods_from_taobao import requests import re import xlsxwriter cok='' ...
随机推荐
- 1093 字符串A+B
给定两个字符串 A 和 B,本题要求你输出 A+B,即两个字符串的并集.要求先输出 A,再输出 B,但重复的字符必须被剔除. 输入格式: 输入在两行中分别给出 A 和 B,均为长度不超过 106 ...
- oracle with和insert结合使用
需求是这样的,先在一个从句中根据sub_code查询dis_code和reg_code, 再把这:两个值作为insert value的一部分,差到rate表里,好了,这里提供一种常规做法,和一种用wi ...
- 第一个Spring 程序
一 搭建好开发环境 JDK Eclipse 等 二 下载jar包 https://commons.apache.org/logging/ https://repo.spring.io/release/ ...
- python类属性和类方法(类的结构、实例属性、静态方法)
类属性和类方法 目标 类的结构 类属性和实例属性 类方法和静态方法 01. 类的结构 1.1 术语 —— 实例 使用面相对象开发,第 1 步 是设计 类 使用 类名() 创建对象,创建对象 的动作有两 ...
- JavaScript的使用你知道几种?(上)
往期回顾 在上一期的<JavaScript的组成 | DOM/BOM>☜里,我们有对文档对象模型-DOM.浏览器对象模型-BOM 这两大部分进行了解学习,如果有还不是很明白的小伙伴们,可以 ...
- php源码学习——开篇
这个系列是对php源码的学习记录.由于本人水平有限,可能并不能写的非常清晰和深入,所以,可能只适合本人阅读:) 初次接触php源码,看到陌生的文件夹和大量的文件,可能会觉得茫然无措.php-inter ...
- FCC JS基础算法题(3):Find the Longest Word in a String (找出最长单词)
题目描述: 在句子中找出最长的单词,并返回它的长度.函数的返回值应该是一个数字. 基本思路,将字符串转换成数组,然后得出数组中单个元素的长度,对长度进行排序,返回最大的一个 代码: function ...
- apk签名的流程
最后总结一下apk签名的整个流程: 一.对Apk中的每个文件做一次算法(数据SHA1摘要+Base64编码),保存到MANIFEST.MF文件中 二.对MANIFEST.MF整个文件做一次算法(数据S ...
- javascript 运算符优先级
JavaScript 运算符优先级(从高到低) https://github.com/xhlwill/blog/issues/16 今天把js函数转换为python 函数时,发现在js运算符优先级这边 ...
- es6学习日记1
1.let和const ES6新增了let命令,用来声明变量,用法类似于var ,但是声明的变量只在let命令所在代码块内有效. 例如: { let a = 10; var b = 1; } a // ...