您当前的位置: 首页 > 

lootaa

暂无认证

  • 2浏览

    0关注

    68博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

亚马逊国际站通过ASIN获取商品信息

lootaa 发布时间:2021-10-14 01:13:03 ,浏览量:2

目录

亚马逊中国站获取全部商品分类 亚马逊中国站获取商品列表 亚马逊中国站通过ASIN获取商品信息 亚马逊中国站获取商品库存信息 亚马逊国际站获取全部商品分类 亚马逊国际站获取商品列表 亚马逊国际站处理图形验证码 亚马逊国际站通过ASIN获取商品信息 亚马逊国际站获取商品库存信息

所提供代码已经为可运行代码,但亚马逊响应数据随时会变,造成解析异常。如果使用期间遇到问题,欢迎随时沟通。可扫描下方二维码公众号留言。 在这里插入图片描述

声明

该方法也可能出现验证码问题,详细参照验证码处理篇,本篇不再赘述。如果仅仅是为了测试,可以简单的只修改user-agent,这样就不会出现验证码了。

两种方式介绍

通过ASIN获取商品信息至少有两种方式,第一种是进入商品详情页,第二种是通过搜索得到商品信息。

进入商品详情页

拼接访问链接:https://www.amazon.com/dp/ + asin编码,进入的页面就是商品详情页面。通过不同的标签获取到商品信息。 该方法有个小问题,亚马逊不同的商品信息详情页是不相同的,需要匹配多种情况。优点是详细信息都有,所有数据都可以拿到。

搜索ASIN搜索

拼接搜索结果链接:https://www.amazon.com/s?k= + asin编码,查询出来的结果就是对应asin编码的商品信息,和商品列表中获取到的信息基本上一致。优点是格式统一,方便获取数据;缺点是信息有限,部分数据拿不到,只能拿到列表上有的信息。同时可能出现多个推广商品,需要匹配下ASIN值是否相等。

打印的数据

测试两种方式打印的数据,ASIN编码使用的“B09B52572Z”,替换了几个也能成功。

名称:iPhone 充电器,5 件装(10 英尺)VODRAIS [Apple MFi 认证] 充电器闪电转 USB 数据线兼容 iPhone 12/11 Pro/11/XS MAX/XR/8/7/6s/6/plus,iPad Pro/Air/Mini,iPod Touch 原始认证-白色
秒杀价:US$8.46
价格:US$10.99
========================
商品详情:https://www.amazon.cn/-/zh/dp/B09B52572Z/ref=sr_1_1?dchild=1&keywords=B09B52572Z&qid=1634144776&sr=8-1
ASIN:B09B52572Z
UUID:bc4afaaf-65f1-4dd7-ab65-d5b72622d6f9
封面图片:https://m.media-amazon.com/images/I/51Ukp-HksKL._AC_UL320_.jpg
名称:iPhone 充电器,5 件装(10 英尺)VODRAIS [Apple MFi 认证] 充电器闪电转 USB 数据线兼容 iPhone 12/11 Pro/11/XS MAX/XR/8/7/6s/6/plus,iPad Pro/Air/Mini,iPod Touch 原始认证-白色
评分:4.5 颗星,最多 5 颗星
评价人数:3036
价格:US$8.46
Java代码

注意,如果不携带中文首页的cookie,打印出来的内容就会是英文。为了显示中文,代码中都是先进入了一次中文首页,然后再携带cookie继续请求。

import java.util.Objects;

import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class AmazonTest8 {

	public static void main(String[] args) throws Exception {
		printInfo1("B09B52572Z");
		
		System.out.println("========================");
		
		printInfo2("B09B52572Z");
	}
	
	static void printInfo1(String asin) throws Exception {
		CookieStore store = new BasicCookieStore();
		CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(store).build();
		HttpGet get = new HttpGet("https://www.amazon.com/?language=zh_CN");
		get.addHeader("user-agent",
						"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1");
		get.addHeader("accept-language", "zh-CN,zh;q=0.9,en;q=0.8");
		httpclient.execute(get);
		
		get = new HttpGet("https://www.amazon.com/dp/" + asin);
		get.addHeader("accept-language", "zh-CN,zh;q=0.9,en;q=0.8");
		get.addHeader("user-agent",
				"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1");
		CloseableHttpResponse rese = httpclient.execute(get);
		String redsa = EntityUtils.toString(rese.getEntity());
		Document doc = Jsoup.parse(redsa);
		
		String title = doc.getElementById("productTitle").text();
		System.out.println("名称:" + title);
		
		Element priceEle = doc.getElementById("priceblock_saleprice");
		if(priceEle == null) {
			priceEle = doc.getElementById("mbc-price-1");
			if(doc.getElementById("priceblock_dealprice") != null) {
				System.out.println("秒杀价:" + doc.getElementById("priceblock_dealprice").text());
			}
		}
		if(priceEle != null) {
			String price = priceEle.text();
			System.out.println("价格:" + price);
		}
	}
	
	static void printInfo2(String asin) throws Exception {
		CookieStore store = new BasicCookieStore();
		CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(store).build();
		HttpGet get = new HttpGet("https://www.amazon.com/?language=zh_CN");
		get.addHeader("user-agent",
						"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1");
		get.addHeader("accept-language", "zh-CN,zh;q=0.9,en;q=0.8");
		httpclient.execute(get);
		
		get = new HttpGet("https://www.amazon.com/s?k=" + asin);
		get.addHeader("accept-language", "zh-CN,zh;q=0.9,en;q=0.8");
		get.addHeader("user-agent",
				"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1");
		CloseableHttpResponse rese = httpclient.execute(get);
		String redsa = EntityUtils.toString(rese.getEntity());
		Document doc = Jsoup.parse(redsa);
		
		Elements goodsEles = doc.getElementsByClass("sg-col-4-of-12 s-result-item s-asin sg-col-4-of-16 sg-col s-widget-spacing-small sg-col-4-of-20");
		Element goodsEle = null;
		for(int i = 0; i < goodsEles.size(); i++) {
			String asins = goodsEles.get(i).attr("data-asin");
			if(Objects.equals(asins, asin)) {
				goodsEle = goodsEles.get(i);
				break;
			}
		}
		
		String detailUrl = "https://www.amazon.cn" + goodsEle.getElementsByTag("a").first().attr("href");
		System.out.println("商品详情:" + detailUrl);
		
		String asins = goodsEle.attr("data-asin");
		System.out.println("ASIN:" + asins);
		
		String uuid = goodsEle.attr("data-uuid");
		System.out.println("UUID:" + uuid);
		
		String img = goodsEle.getElementsByTag("img").first().attr("src");
		System.out.println("封面图片:" + img);
		
		String subTitle = goodsEle.getElementsByTag("h2").first().text();
		System.out.println("名称:" + subTitle);
		
		Element starEle = goodsEle.getElementsByClass("a-icon-alt").first();
		if(starEle != null) {
			String star = starEle.text();
			System.out.println("评分:" + star);
			
			String count = goodsEle.getElementsByClass("a-section a-spacing-none a-spacing-top-micro").first().getElementsByClass("a-size-base").first().text().replaceAll(",", "");
			System.out.println("评价人数:" + count);
		} else {
			System.out.println("暂无评分");
			System.out.println("评价人数:0");
		}
		
		String price = goodsEle.getElementsByClass("a-offscreen").first().text().replaceAll(",", "");
		System.out.println("价格:" + price);
		
	}
	
}
关注
打赏
1663829960
查看更多评论
立即登录/注册

微信扫码登录

0.0621s