• 爬虫抓取图片


     ***********************//前台/************************

     <asp:Button ID="Button2" runat="server" Text="省市图片抓取" OnClick="Button2_Click" />

    *****************************//////后台/********************************

    protected void Button2_Click(object sender, EventArgs e)
    {
    //查寻数据库,城市表的城市名字
    string connstring = ConfigurationManager.ConnectionStrings["sqlconn"].ConnectionString;
    SqlConnection conn = new SqlConnection(connstring);
    SqlDataAdapter sda = new SqlDataAdapter("select top 100 * from M_City ", conn);
    DataSet ds = new DataSet();
    sda.Fill(ds);
    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
    Directory.CreateDirectory(@"C:UsersAdministrator.PC-201809300840DesktopCityList" + ds.Tables[0].Rows[i]["CityName"] + "");
    HtmlWeb webClient = new HtmlWeb();
    //访问图片的地址网站,
    HtmlAgilityPack.HtmlDocument doc = webClient.Load("http://soso.nipic.com/q_" + ds.Tables[0].Rows[i]["CityName"] + "_g_0.html");
    HtmlNodeCollection html = doc.DocumentNode.SelectNodes(".//li[@class='new-search-works-item']");
    //electSingleNode("/html/body/div[4]/div[1]/div[2]/ul[1]");
    // .//div[@class='search-works-wrap none']/div[@class='search-works-box clearfix']/div[@class='f1']/ul[@class='clearfix']/li[@class='new-search-work-item']
    //创建文件夹
    string strs = @"C:UsersAdministrator.PC-201809300840DesktopCityList" + ds.Tables[0].Rows[i]["CityName"] + "" + "\";

    if (html != null)
    {
    for (int j = 1; j < 10; j++)
    {
    //图片的URL地址
    var names = html[j].Elements("a").ToList()[0].Elements("img").ToList()[0].Attributes["data-original"].Value;


    WebRequest imgRequest = WebRequest.Create(names);
    HttpWebResponse res;
    try
    {
    res = (HttpWebResponse)imgRequest.GetResponse();
    }
    catch (WebException ex)
    {
    res = (HttpWebResponse)ex.Response;
    }
    if (res.StatusCode.ToString() == "OK")
    {
    System.Drawing.Image dwonImage = System.Drawing.Image.FromStream(imgRequest.GetResponse().GetResponseStream());
    //图片名字

    string picname = Path.GetFileName(names);
    //dwonImage.Dispose();
    dwonImage.Save(strs+picname);
    dwonImage.Dispose();

    }


    //HtmlAttribute name = doc.DocumentNode.SelectSingleNode(".//li[@class='new-search-works-item']/a[1]").Attributes["href"];

    }
    }

    }

    }

    **************//配值数据库的链接1////******

    <connectionStrings>
    <add name="sqlconn" connectionString="server=PC-201809300840;uid=sa;pwd=sa;database=ceshi" />
    </connectionStrings>

    **************//配值数据库的链接2////******

    <appSettings>
    <add key="conn" value="server=PC-201809300840;uid=sa;pwd=sa;database=ceshi"/>
    </appSettings>

  • 相关阅读:
    Linux中find常见用法示例
    PHP写的异步高并发服务器,基于libevent
    Prototype原型模式
    抽象工厂模式
    工厂方法模式
    简单工厂模式
    记一次皮肤过敏
    Thread类线程结束会唤醒使用其对象做锁而睡眠的线程
    位运算符
    逻辑运算符
  • 原文地址:https://www.cnblogs.com/qqhewei/p/10750286.html
Copyright © 2020-2023  润新知