c#抓取亚马逊商品信息①获取商品详情页信息

获取分类排名

    List<Tuple<string, int>> sortRankingList = new List<Tuple<string, int>>();
    string asin = "";

    string url = "https://www.amazon.com/gp/product/" + asin;

    string strHTML = "";
    WebClient myWebClient = new WebClient();
    Stream myStream = myWebClient.OpenRead(url);
    StreamReader sr = new StreamReader(myStream, System.Text.Encoding.GetEncoding("utf-8"));
    strHTML = sr.ReadToEnd();
    myStream.Close();
    
    if (strHTML.IndexOf("Amazon Best Sellers Rank") != -1)
    {
        strHTML = strHTML.Substring(strHTML.IndexOf("Amazon Best Sellers Rank"));
        strHTML = strHTML.Substring(0, strHTML.IndexOf("</div>"));

        string newStrHtml = "";
        newStrHtml += "<!DOCTYPE html>" + System.Environment.NewLine;
        newStrHtml += "<html lang='en-us' class='a-no-js' data-19ax5a9jf='dingo'>" + System.Environment.NewLine;
        newStrHtml += "<head><title></title><meta charset='utf-8'/><meta http-equiv='x-dns-prefetch-control' content='on'><link rel='dns-prefetch' href='https://images-na.ssl-images-amazon.com'><link rel='dns-prefetch' href='https://m.media-amazon.com'><link rel='dns-prefetch' href='https://completion.amazon.com'><script type='text/javascript'>var ue_t0=ue_t0||+new Date();</script></head>" + System.Environment.NewLine;
        newStrHtml += "<body>" + System.Environment.NewLine;
        newStrHtml += "<ul><li>" + strHTML + "</ul>" + System.Environment.NewLine;
        newStrHtml += "</body></html>";

        HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
        doc.LoadHtml(newStrHtml);

        var nodes = doc.DocumentNode.SelectNodes(".//li");
        if (nodes != null)
        {
            for (int i = 0; i < nodes.Count; i++)
            {
                string sortName = "";
                int ranking = 0;

                if (i == 0)
                {
                    string nodeText = nodes[i].InnerText;
                    nodeText = nodeText.Substring(nodeText.IndexOf("#"));
                    nodeText = nodeText.Substring(0, nodeText.IndexOf("in"));
                    
                    sortName = nodes[i].SelectSingleNode(".//a[1]") != null ? nodes[i].SelectSingleNode(".//a[1]").InnerText : "";
                    sortName = sortName.Replace("See Top 100 in ", "");
                    ranking = int.Parse(nodeText.Replace("#", "").Replace(",", "").Trim());
                }
                else
                {
                    sortName = nodes[i].SelectSingleNode(".//a") != null ? nodes[i].SelectSingleNode(".//a").InnerText : "";
                    var rankStr = nodes[i].SelectSingleNode(".//span[1]") != null ? nodes[i].SelectSingleNode(".//span[1]").InnerText : "";
                    ranking = int.Parse(rankStr.Replace("#", "").Replace(",", "").Trim());
                }

                if (string.IsNullOrEmpty(sortName) == false)
                {
                    sortRankingList.Add(new Tuple<string, int>(sortName, ranking));
                }
            }
        }
    }
    else if (strHTML.IndexOf("Best Sellers Rank") != -1)
    {
        strHTML = strHTML.Substring(strHTML.IndexOf("Best Sellers Rank"));
        strHTML = strHTML.Substring(0, strHTML.IndexOf("</tr>"));

        string newStrHtml = "";
        newStrHtml += "<!DOCTYPE html>" + System.Environment.NewLine;
        newStrHtml += "<html lang='en-us' class='a-no-js' data-19ax5a9jf='dingo'>" + System.Environment.NewLine;
        newStrHtml += "<head><title></title><meta charset='utf-8'/><meta http-equiv='x-dns-prefetch-control' content='on'><link rel='dns-prefetch' href='https://images-na.ssl-images-amazon.com'><link rel='dns-prefetch' href='https://m.media-amazon.com'><link rel='dns-prefetch' href='https://completion.amazon.com'><script type='text/javascript'>var ue_t0=ue_t0||+new Date();</script></head>" + System.Environment.NewLine;
        newStrHtml += "<body>" + System.Environment.NewLine;
        newStrHtml += "<table><tr><th>" + strHTML + "</tr></table>" + System.Environment.NewLine;
        newStrHtml += "</body></html>";

        HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
        doc.LoadHtml(newStrHtml);

        var nodes = doc.DocumentNode.SelectNodes(".//span/span");
        if (nodes != null)
        {
            for (int i = 0; i < nodes.Count; i++)
            {
                string sortName = "";
                int ranking = 0;

                sortName = nodes[i].SelectSingleNode(".//a") != null ? nodes[i].SelectSingleNode(".//a").InnerText : "";
                sortName = sortName.Replace("See Top 100 in ", "");

                var rankStr = nodes[i].InnerText;
                rankStr = rankStr.Substring(0, rankStr.IndexOf("in"));
                ranking = int.Parse(rankStr.Replace("#", "").Replace(",", "").Trim());

                if (string.IsNullOrEmpty(sortName) == false)
                {
                    sortRankingList.Add(new Tuple<string, int>(sortName, ranking));
                }
            }
        }
    }

总评分、review数量

    string asin = "";
    string url = "https://www.amazon.com/gp/product/" + asin;
    string strHTML = "";
    WebClient myWebClient = new WebClient();
    Stream myStream = myWebClient.OpenRead(url);
    StreamReader sr = new StreamReader(myStream, System.Text.Encoding.GetEncoding("utf-8"));
    strHTML = sr.ReadToEnd();
    myStream.Close();

    HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
    doc.LoadHtml(strHTML);

    HtmlNode comment_star_node = doc.DocumentNode.SelectSingleNode("//span[@id='acrPopover']");
    string comment_star_str = comment_star_node == null ? "" : comment_star_node.Attributes["title"].Value;

    HtmlNode comment_review_node = doc.DocumentNode.SelectSingleNode("//span[@id='acrCustomerReviewText']");
    string comment_review_str = comment_review_node == null ? "" : comment_review_node.InnerText;

    //截取总评分和review数量
    if (string.IsNullOrEmpty(comment_star_str) == false)
    {
        double star = double.Parse(comment_star_str.Replace("out of 5 stars", "").Trim());
    }

    if (string.IsNullOrEmpty(comment_review_str) == false)
    {
        int review = int.Parse(comment_review_str.Replace("ratings", "").Trim());
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
有源码,有文档,开发环境是在Visual Studio .NET 2005的平台上C#.Net的应用程序(winform),采用SQL Server 2000数据库系为基本应用软件开发系统程序。 第一章 前言 1 1.1 项目依据及意义 1 1.2 国内外研究现状及发展趋势 1 1.3 项目内容及技术分析 2 1.4 本课题研究方案 2 1.5 研究目标、主要特色 3 第二章 可行性研究 4 2.1 经济可行性 4 2.2 社会可行性 4 2.3 技术可行性 4 第三章 需求分析 6 3.1 系统功能需求 6 3.2. 系统非功能性需求 7 3.2.1性能需求 7 3.2.2 安全性需求 7 3.3 应用与开发环境 8 3.3.1 系统定义 8 3.3.2 运行环境 8 3.3.3 硬件环境 8 3.3.4 其他要求 9 第四章 总体设计 10 4.1 总体设计概述 10 4.2 系统功能结构设计 10 4.3 系统各子系统的分析与设计 11 4.3.1 用户登录子系统 11 4.3.2商品管理系统基本概念模型 11 4.4数据库中主要表的结构设计 13 4.4.1 进货计划表的结构设计 13 4.4.2 商品库存表 14 4.4.3 用户表 15 4.4.4 人员管理表 15 4.4.5 三个自由表情况 16 第五章 详细设计与规划 17 5.1系统登录 17 5.2系统管理 19 5.2.1 用户管理 19 5.2.2 密码管理 21 5.3 商品管理 23 5.3.1 进货管理 23 5.3.2 库存管理 25 5.3.3 商品管理 26 5.4 供货商管理 27 5.4.1 供货商管理 27 5.4.2 供货商查询和统计 29 5.5销售管理 30 第六章 系统测试与调试 33 6.1 单元测试 33 6.2 综合测试 34 6.3 验收测试 34 第七章 系统的维护 35 第八章 总结 36 致 谢 37

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值