爬虫抓取图片

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

 <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:\Users\Administrator.PC-201809300840\Desktop\CityList\" + 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:\Users\Administrator.PC-201809300840\Desktop\CityList\" + 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>

posted @ 2019-04-22 15:15  自不量力  阅读(502)  评论(0编辑  收藏  举报