抓取淘宝天猫的商品的促销价格
来源:互联网 发布:晨曦造价软件下载 编辑:程序博客网 时间:2024/04/28 08:55
通过商品的url获取促销价,天猫淘宝的促销价并不是直接生成的,而是通过js间接生成的。所以通过jsoup等工具无法抓取。
首先是尝试使用htmlUnit,因为其可以,模拟浏览器运行js、css。经试验,htmlunit确实可以运行js,尝试抓取我自己的网站,可成功获取ajax、js生成的内容。但在抓取淘宝网站时出现异常,htmlunit对网站内容的格式有一定的要求,于是只能想其他办法(以后再尝试此方法)。
接着使用httpwatch分析淘宝的请求,发现其中一个请求返回的值中有促销价的信息。
这个请求有2个关键点,关键1:参数itemid(就是商品的id)。关键2:是请求header的Referer。
具体代码如下:
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.util.HashMap;import java.util.Map;import org.apache.http.HttpResponse;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.DefaultHttpClient;public class Test4Taobao {public static void main(String[] args) throws ClientProtocolException, IOException {HttpClient httpclient = new DefaultHttpClient();String url="http://detail.tmall.com/item.htm?spm=a230r.1.14.44.RoTYht&id=43508885384&ns=1&abbucket=20";String id=getStrByPrePost(url, "id=", "&");//根据url获取商品的idif(id!=null){//组装出获取商品信息的urlString detailUrl="http://mdskip.taobao.com/core/initItemDetail.htm?itemId="+id+"×tamp=1429065419751&tgTag=false&cartEnable=true&addressLevel=2&progressiveSupport=false&isUseInventoryCenter=false&household=false¬AllowOriginPrice=false&isRegionLevel=false&sellerUserTag=303632416&sellerUserTag2=18020085046181888&tmallBuySupport=true&isAreaSell=false&queryMemberRight=true&isIFC=false&sellerUserTag3=70368779862144&sellerUserTag4=8800522208643&service3C=true&tryBeforeBuy=false&isSecKill=false&isForbidBuyItem=false&sellerPreview=false&itemTags=843,1163,1478,1483,1547,1611,1803,1867,2049,2059,2443,2507,2635,3787,3974,4166,4555,4811,5323,21762,21826,25282,28802,29122,34178,56130,56194&isApparel=false&offlineShop=false&showShopProm=false&callback=setMdskip";HttpGet get = new HttpGet(detailUrl);get.addHeader("Referer", url);//Referer必须要设置HttpResponse response = httpclient.execute(get);String content = getContent(response);content=getStrByPrePost(content, "setMdskip(", ")");//返回的内容是json格式,可以通过gson等来解析,为了方便,此处直接截取。System.out.println("商品信息:"+content);String promotionPrice = getStrByPrePost(content, "promotionList", null);promotionPrice = getStrByPrePost(promotionPrice, "\"price\":", ",");System.out.println("商品价格:"+promotionPrice);}}/** * 获取返回内容的string * @param response * @return * @throws IllegalStateException * @throws IOException */private static String getContent(HttpResponse response) throws IllegalStateException, IOException {java.io.InputStream is = response.getEntity().getContent();BufferedReader br = new BufferedReader(new InputStreamReader(is, "GBK"));//response.getEntity().getContentEncoding().getName()String line = "";String temp= null;while ((temp = br.readLine()) != null) {line+=temp;}return line;}/** * 根据前后的文本截取指定的内容。 * @param str * @param pre * @param post * @return */public static String getStrByPrePost(String str,String pre,String post){if(str!=null){if(pre!=null){int s = str.indexOf(pre);if(s>-1){str = str.substring(s+pre.length(), str.length());}else{return null;}}if(post!=null){int e = str.indexOf(post);if(e>-1){str = str.substring(0,e);}else{return null;}}}return str;}}第一篇博客
to be continued
0 0
- 抓取淘宝天猫的商品的促销价格
- java爬虫抓取天猫商品的价格数据
- 淘宝客获取促销价格API的示例
- 利用nodejs+phantomjs+casperjs采集淘宝商品的价格
- 利用nodejs+phantomjs+casperjs采集淘宝商品的价格
- 利用nodejs+phantomjs+casperjs采集淘宝商品的价格
- 爬取淘宝商品的价格(移动端网页)
- 求助:关于淘宝商品详情数据的抓取问题
- 问题一百二十六:促销的价格
- 价格歧视:降价促销的底层逻辑
- 【C++】促销商品的折扣计算
- 二分法猜测商品的价格
- php采集淘宝商品价格,促销价格
- 【爬虫】获取淘宝天猫商品的详细参数
- 最新抓取淘宝价格数据,图片标题的就更好抓了整合就好
- ecshop中考虑促销价格的排序方式
- 游戏里的打折促销和低廉价格
- 模拟节目中猜商品的价格
- 最少步数
- 多线程和同步之AsyncTask的使用(二)
- 宏定义中的#,##
- 个人投资理财哪些方法比较靠谱
- 使用java中的关键字(如: delete)用作数据库表中的字段名时,如何取数据
- 抓取淘宝天猫的商品的促销价格
- 使用Image.xcassets设置ios启动图和图标
- cocos2dx lua_UIWebView_auto.hpp webView tolua 文件
- AsyncTask的用法
- springmvc 视图解析器的学习
- delphi控件安装与删除
- 编译出现 error: unrecognized command line option "-fconserve-stack"
- cocos2dx lua_UIWebView_auto.cpp webView tolua实现文件
- Android高效加载大图、多图解决方案,有效避免程序OOM