抓取淘宝天猫的商品的促销价格
2015-04-15 13:32
1981 查看
通过商品的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获取商品的id if(id!=null){ //组装出获取商品信息的url String 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
相关文章推荐
- java爬虫抓取天猫商品的价格数据
- 抓取淘宝商品价格人气-http://hws.m.taobao.com/cache/wdetail/5.0
- 抓取淘宝某类商品名称和价格
- 通过交互式网页爬取,抓取天猫商品价格
- 获取页面js动态写入的数据(抓取天猫商品价格)
- 【实例】python 使用beautifulSoup 抓取网页正文 以淘宝商品价格为例
- php采集淘宝商品价格,促销价格
- 简单的抓取淘宝关键字信息、图片的Python爬虫|Python3中级玩家:淘宝天猫商品搜索爬虫自动化工具(第二篇)
- scrapy-抓取天猫女装销量前60的商品名称、价格、链接及打开连接后的店铺名称和链接
- php实现抓取淘宝商品价格人气源码
- 在iOS应用中跳转到淘宝或天猫客户端商品详情页
- Python爬虫(一)京东商品价格及详情页抓取
- Python网页信息采集:使用PhantomJS采集淘宝天猫商品内容
- Python网络爬虫获取淘宝商品价格
- Python实例之抓取淘宝商品数据(json型数据)并保存为TXT
- Python使用Selenium模块实现模拟浏览器抓取淘宝商品美食信息功能示例
- python3下几个淘宝、天猫、京东爬虫实例。(价格、销量、评论等)
- 爬取淘宝商品的价格(移动端网页)
- PHP抓取淘宝商品的用户晒单评论+图片+搜索商品列表实例
- 【爬虫】获取淘宝天猫商品的详细参数