栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

前述

亚马逊中国站获取全部商品分类:https://blog.csdn.net/m0_58095675/article/details/120624031
亚马逊中国站获取商品列表:https://blog.csdn.net/m0_58095675/article/details/120629322

两种方式介绍

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

进入商品详情页

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

搜索ASIN搜索

拼接搜索结果链接:https://www.amazon.cn/s?k= + asin编码,查询出来的结果就是对应asin编码的商品信息,和商品列表中获取到的信息基本上一致。优点是格式统一,方便获取数据;缺点是信息有限,部分数据拿不到,只能拿到列表上有的信息。

打印的数据

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

名称:Clek Foonf 饮料杯套
价格:¥163.56
========================
商品详情:https://www.amazon.cn/dp/B00HB5ZOW6/ref=sr_1_1?dchild=1&keywords=B00HB5ZOW6&qid=1633527509&sr=8-1
ASIN:B00HB5ZOW6
UUID:01c5a223-c899-4e6d-8690-c6d8acc754f7
封面图片:https://images-cn.ssl-images-amazon.cn/images/I/71DtM5E-enL._AC_UL320_.jpg
名称:Clek Foonf 饮料杯套
评分:4.0 颗星,最多 5 颗星
评价人数:453
价格:¥163.56
Java代码
import org.jsoup.Jsoup;
import org.jsoup.nodes.document;
import org.jsoup.nodes.Element;

public class AmazonTest3 {

	public static void main(String[] args) throws Exception {
		printInfo1("B07X43GJ1L");
		
		System.out.println("========================");
		
		printInfo2("B07X43GJ1L");
	}
	
	static void printInfo1(String asin) throws Exception {
		document doc = Jsoup.connect("https://www.amazon.cn/dp/" + asin)
				.header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36")
				.get();
		String title = doc.getElementById("productTitle").text();
		System.out.println("名称:" + title);
		
		String price = doc.getElementById("priceblock_ourprice").text();
		System.out.println("价格:" + price);
	}
	
	static void printInfo2(String asin) throws Exception {
		document doc = Jsoup.connect("https://www.amazon.cn/s?k=" + asin)
				.header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36")
				.get();
		
		Element goodsEle = doc.getElementsByClass("sg-col-4-of-12 s-result-item s-asin sg-col-4-of-16 sg-col sg-col-4-of-20").first();
		
		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);
		
	}
	
	
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/298825.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号