Exemplo n.º 1
0
        private void btn_caiji1_Click(object sender, EventArgs e)
        {
            GC.Collect();
            dgv1.Rows.Clear();
            string str = txt_key.Text.Trim();


            //判断是否已经选择采集店铺
            string temp_dp = cb_dianpu.SelectedValue.ToString();

            if (!string.IsNullOrEmpty(temp_dp))
            {
                IList <opponent_dianpu> temp_listdp2 = BLL2.opponent_dianpuManager.Search(1, 100, temp_dp, "");
                if (temp_listdp2.Count > 0)
                {
                    str = temp_listdp2[0].odwangwang;
                }
            }



            //str = "https://item.taobao.com/item.htm?spm=a1z10.3-c-s.w4002-14460596614.23.XM7KUF&id=540733715384";//sunny家网址
            if (string.IsNullOrEmpty(str))
            {
                MessageBox.Show("请输入要分析的店铺");
                return;
                //str = "miss原创定制";
            }
            //判断是否是网址
            int    temp_isurl = str.IndexOf("http");
            string id         = Manager.GetValueByURL(str, "id");
            string datenow    = DateTime.Now.ToString("yyyy年MM月dd日HH时mm分");

            //如果是网址
            if (temp_isurl > -1 && !string.IsNullOrEmpty(id))
            {
                //string url = Manager.GetDPurlByWangwang(str);
                Browser.urlstr = str;
                webBrowser1.Navigate(Browser.urlstr);
                //判断是否加载完成
                if (Browser.WaitWebPageLoad(webBrowser1))
                {
                    try
                    {
                        if (webBrowser1.Document != null)
                        {
                            //判断是否是邮费链接
                            HtmlElementCollection elem1 = webBrowser1.Document.GetElementById("attributes").GetElementsByTagName("ul")[0].GetElementsByTagName("li");

                            if (elem1.Count > 0)
                            {
                                //string datenow = DateTime.Now.ToString("yyyy年MM月dd日HH时mm分");
                                //先采集商品
                                opponent_goods        og   = CaiJi.CaijiGoods(datenow, webBrowser1);
                                List <opponent_goods> list = new List <opponent_goods>();
                                if (og != null)
                                {
                                    list.Add(og);
                                    dgv1.DataSource = Manager.BindingSort_opponent_goods(list);
                                    //再采集店铺信息,需要跳转页面
                                    Entity.opponent_dianpu dp = CaiJi.CaijiDP(datenow, webBrowser1);
                                    BLL2.opponent_dianpuManager.Insert(dp);

                                    opponent_dianpu_info dpinfo = CaiJi.CaijiDPSDR(datenow, webBrowser1);
                                    dpinfo = bind_dianpu_info(list, dpinfo);
                                    BLL2.opponent_dianpu_infoManager.Insert(dpinfo);

                                    BLL2.opponent_goodsManager.Insert(og);
                                    GC.Collect();//释放内存
                                }
                            }
                            else
                            {
                                Debug.WriteLine("======================================================================");
                                Debug.WriteLine("网址:" + Browser.urlstr);
                                Debug.WriteLine("该商品可能是 邮费链接 ");
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        //MessageBox.Show("采集信息失败:" + ex.Message);
                        Debug.WriteLine("======================================================================");
                        Debug.WriteLine("网址:" + Browser.urlstr);
                        Debug.WriteLine(DateTime.Now.ToString());
                        Debug.WriteLine("单品采集信息失败:" + ex.Message);
                    }
                }
            }
            else
            {
                //如果不是网址
                string url = CaiJi.GetShopSearchURL(str);
                url = Browser.UrlEncode(url, Encoding.UTF8);
                //Debug.WriteLine(url);
                //跳转到搜索店铺页面
                Browser.urlstr = url;
                webBrowser1.Navigate(Browser.urlstr);
                //判断是否加载完成
                if (Browser.WaitWebPageLoad(webBrowser1))
                {
                    try
                    {
                        if (webBrowser1.Document != null)
                        {
                            string url_shop = CaiJi.GetShopUrlByWangWang(str, webBrowser1);
                            //如果没有找到店铺网址,或者没有开店信息
                            if (string.IsNullOrEmpty(url_shop))
                            {
                                Debug.WriteLine("采集店铺时,没有找到店铺网址,或者没有开店信息");
                                MessageBox.Show("没有采集到该店铺信息");
                                return;
                            }
                            List <string>         list_url   = new List <string>();
                            List <opponent_goods> list_goods = new List <opponent_goods>();

                            //默认查询20页
                            for (int i = 0; i < 20; i++)
                            {
                                string url_goodslist = CaiJi.GetShopGoodsListURL(url_shop, i + 1);
                                Browser.urlstr = url_goodslist;
                                //跳转到商品列表页面
                                webBrowser1.Navigate(Browser.urlstr);
                                //break;
                                //判断是否加载完成
                                if (Browser.WaitWebPageLoad(webBrowser1))
                                {
                                    if (webBrowser1.Document != null)
                                    {
                                        //判断是否还有商品
                                        Browser.jsstr = "  getClassName('no-result-new').length;  ";
                                        if (Browser.JS_WebBrowser(webBrowser1) == "1")
                                        {
                                            break;
                                        }
                                        else
                                        {
                                            //================
                                            //list_url = CaiJi.GetGoodsUrlList(webBrowser1);
                                            //获取本页面商品网址List
                                            List <string> temp_list_url = CaiJi.GetGoodsUrlList(webBrowser1);
                                            Debug.WriteLine("获取本页面商品网址List" + temp_list_url.Count.ToString());
                                            if (temp_list_url.Count > 0)
                                            {
                                                foreach (string temp_url1 in temp_list_url)
                                                {
                                                    Browser.urlstr = temp_url1;
                                                    //跳转到商品页面
                                                    webBrowser1.Navigate(Browser.urlstr);
                                                    if (Browser.WaitWebPageLoad(webBrowser1))
                                                    {
                                                        if (webBrowser1.Document != null)
                                                        {
                                                            //判断是否是邮费链接
                                                            HtmlElementCollection elem1 = webBrowser1.Document.GetElementById("attributes").GetElementsByTagName("ul")[0].GetElementsByTagName("li");

                                                            if (elem1.Count > 0)
                                                            {
                                                                opponent_goods og = CaiJi.CaijiGoods(datenow, webBrowser1);
                                                                if (og != null)
                                                                {
                                                                    list_goods.Add(og);
                                                                    dgv1.DataSource = Manager.BindingSort_opponent_goods(list_goods);
                                                                    BLL2.opponent_goodsManager.Insert(og);
                                                                    Debug.WriteLine("添加商品:" + og.ogtittle);

                                                                    //绑定店铺信息
                                                                    opponent_dianpu_info dpinfo2 = new opponent_dianpu_info();
                                                                    dpinfo2 = bind_dianpu_info(list_goods, dpinfo2);

                                                                    GC.Collect();//释放内存
                                                                }
                                                            }
                                                            else
                                                            {
                                                                Debug.WriteLine("======================================================================");
                                                                Debug.WriteLine("网址:" + Browser.urlstr);
                                                                Debug.WriteLine("该商品可能是 邮费链接 ");
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                            dgv1.DataSource = Manager.BindingSort_opponent_goods(list_goods);

                            //在本页获取店铺信息
                            opponent_dianpu dp = CaiJi.CaijiDP(datenow, webBrowser1);
                            //在本页获取店铺信息
                            opponent_dianpu_info dpinfo = CaiJi.CaijiDPSDR(datenow, webBrowser1);
                            //从商品列表获取店铺信息

                            dpinfo = bind_dianpu_info(list_goods, dpinfo);


                            BLL2.opponent_dianpu_infoManager.Insert(dpinfo);
                            BLL2.opponent_dianpuManager.Insert(dp);


                            GC.Collect();//释放内存
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("======================================================================");
                        Debug.WriteLine("网址:" + Browser.urlstr);
                        Debug.WriteLine(DateTime.Now.ToString());
                        Debug.WriteLine("全店采集信息失败:" + ex.Message);
                        MessageBox.Show("全店采集信息失败:" + ex.Message);
                    }
                }
            }

            bind_dianpu();
            MessageBox.Show("采集完成");
        }