Beispiel #1
0
        /// <summary>
        /// 通过请求来爬取doc网页的数据信息
        /// </summary>
        /// <returns></returns>
        public async Task <AjaxResponse> GetInfoByUrl()
        {
            string url = Request.Query["url"];

            var content = await HttpTools.GetStringAsync(url);

            //1:创建crawlerOption

            CrawlerOptions options = new CrawlerOptions()
            {
                Content      = content,
                CssSelectors = new List <SelectorOptions>()
                {
                    new SelectorOptions("title", "body > div:nth-child(7) > div > div.itemInfo-wrap > div.sku-name"),
                    new SelectorOptions("desc", "#detail > div.tab-con > div:nth-child(1) > div.p-parameter > ul.parameter2.p-parameter-list"),
                    new SelectorOptions("skuid", "#detail > div.tab-con > div:nth-child(1) > div.p-parameter > ul.parameter2.p-parameter-list > li:nth-child(2)"),
                    new SelectorOptions("imgs", "#spec-list > ul > li > img"),
                }
            };

            var result = await CrawlerTool.GetResultAsync(options);

            var title = result["title"].FirstOrDefault().Text.Trim();
            var desc  = result["desc"].FirstOrDefault().OutHtml;
            //商品编号
            var skuid = result["skuid"].FirstOrDefault()?.Attributes["title"];

            //下载image
            var imgs = result["imgs"];

            List <string> imgList = new List <string>();

            if (imgs.Count > 0)
            {
                imgs.ForEach(u =>
                {
                    //1.得到小图片
                    var imgSrc = u.Attributes["src"];
                    //2.得到大图片
                    var bigImg = "https:" + imgSrc.Replace("n5/s54x54_jfs", "n1/s450x450_jfs");
                    //3.下载图片
                    imgList.Add(bigImg);
                });
            }

            return(await Task.FromResult(new AjaxResponse()
            {
                Result = new { Title = title, Desc = desc, Imgs = imgList, Skuid = skuid }
            }));
        }
Beispiel #2
0
        /// <summary>
        /// 通过skuids来获取商品的价格
        /// </summary>
        /// <param name="skuid"></param>
        /// <param name="product">商品实体</param>
        /// <returns></returns>
        private async Task GetAndSetPrice(string skuid, Mall_Product product)
        {
            string strPrice = await HttpTools.GetStringAsync($"http://p.3.cn/prices/mgets?skuIds={skuid}");

            List <ProductPrice> pList = JsonTools.ToList <ProductPrice>(strPrice);

            decimal nowPrice = Math.Ceiling(pList[0].p);

            if (nowPrice != 0 && nowPrice != product.Price)
            {
                Logger.Info($"商品:{product.Name},原价格:{product.Price},新价格:{nowPrice}");
                product.Price = nowPrice;
            }
            await _productRepository.UpdateAsync(product);
        }
Beispiel #3
0
        public async Task <ActionResult> Login()
        {
            string returnUrl  = Request.Query["returnUrl"];
            string token      = Request.Query["token"];
            string authServer = _appConfiguration["Authorzation:Server"];

            //检查一下请求
            if (string.IsNullOrEmpty(token) && !_env.IsDevelopment())
            {
                return(Redirect($"{authServer}?returnUrl={Request.Scheme}://{Request.Host}{Request.Path}"));
            }
            //如果token不存在,那么就让他先登陆系统
            else
            {
                var gongHao = string.Empty;
                if (!_env.IsDevelopment())
                {
                    //通过token来验证请求
                    var    webApiReq = _appConfiguration["Authorzation:Authentication"];
                    string authUrl   = $"{authServer}{webApiReq}{token}";
                    var    result    = await HttpTools.GetStringAsync(authUrl);

                    if (result == "null")
                    {
                        return(Redirect($"{authServer}"));
                    }
                    gongHao = JsonConvert.DeserializeObject <LoginModel>(result).Account;
                }
                else
                {
                    gongHao = "M0679";
                }
                await LoginAysnc(new LoginModel()
                {
                    Account    = gongHao,
                    Password   = "******",
                    IsRemember = false
                });

                return(Redirect("/Home/Index"));
            }
            //return await Task.FromResult(View());
        }
Beispiel #4
0
        /// <summary>
        /// 创建HtmlDocument
        /// </summary>
        /// <param name="opts"></param>
        /// <returns></returns>
        private static async Task <IHtmlDocument> CreateDocumnet(CrawlerOptions opts)
        {
            var source = string.Empty;

            if (!string.IsNullOrEmpty(opts.Url))
            {
                source = await HttpTools.GetStringAsync(opts.Url);
            }
            if (!string.IsNullOrEmpty(opts.Content))
            {
                source = opts.Content;
            }

            //1.创建一个html解析器
            var parser = new HtmlParser();
            //2.解析数据源
            var document = parser.Parse(source);

            //3.返回document
            return(document);
        }