zoukankan      html  css  js  c++  java
  • 爬虫抓取图片

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

     <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>

  • 相关阅读:
    ffmpeg合并视频+音频
    You-Get和youtube-dl
    【会声会影】初装会声会影时,必要的设置
    【会声会影】视频导出、输出时,如何设置参数
    【会声会影】导入的srt字幕文件,如何快速批量调整字体及大小
    把h264文件快速包装成mp4格式
    Notepad++正则表达式——去掉srt字幕文件的时间轴
    百度地图API的网页使用
    ASPCMS_判断语句if标签的使用
    Grid布局
  • 原文地址:https://www.cnblogs.com/qqhewei/p/10750286.html
Copyright © 2011-2022 走看看