主要是發現GQ在放正妹圖只讓人看,不讓人直接下載,不過進到原始檔內有提供圖檔連結,
所以心血來潮花了一個下午,試著寫一個"抓GQ 的性感女人頁面的圖"的小程式,第一次寫抓圖的程式

學會:

1.如何抓網頁的原始碼

2.如何下載檔案

3.如何找到字串中的某一子字串位置

4.如何取出字串中的子字串


目標:http://www.gq.com.tw/gq-girls/girls/

作法:發現其內頁裡的有小圖點入可以連結至大圖,而且其影像名稱相同,還算仁慈,差別是

小圖:
http://gqimg.fashionsnightout.com.tw//userfiles/sm

/sm120_images_A1/15939/2013101558621297.jpg

大圖:

http://gqimg.fashionsnightout.com.tw//userfiles/sm

/sm1024_images_A1/15939/2013101558621297.jpg


因此就在分頁裡找到所有圖的小圖連結,並將他重組成大圖後,再存起來


介面


未命名  

程式:

編寫按下 "go" 按鈕後的動作

 

private void button1_Click(object sender, EventArgs e)
{
WebClient wc = new WebClient();


//使用webrequest 抓網頁資料


HttpWebRequest httpReq;
HttpWebResponse httpResp;
string strBuff = "";
char[] cbuffer = new char[256];
int byteRead = 0;
int a = 0;
int b = 0;
int c = 0;
int temp = 0;
int count = 0;
int docStart = 0;
int docStop = 0;
bool first = true;

string HTTP="HD9.popup";
string start = "A1/";
string end = "\"";
string filename = @"H:\log.txt";
string subS = "";
string document = "";
string defString = "http://gqimg.fashionsnightout.com.tw//userfiles/sm/sm1024_images_";
string saveName = "";
Uri httpURL = new Uri(textBox1.Text);


//HttpWebRequest 繼承子WebRequest,並沒有自己的構造函式,需通過WebRequest的Creat方法建立,並進行强制的類型轉換


httpReq = (HttpWebRequest)WebRequest.Create(httpURL);
httpResp = (HttpWebResponse)httpReq.GetResponse();


///GetResponseStream()方法获取HTTP响应的数据流,并尝试取得URL中所指定的网页内容
//////若成功取得网页的内容,则以System.IO.Stream形式返回,若失败则产生ProtoclViolationException错 误。在此正确的做法应将以下的代码放到一个try块中处理。这里简单处理


Stream respStream = httpResp.GetResponseStream();
///返回的内容是Stream形式的,所以可以利用StreamReader类获取GetResponseStream的内容,并以StreamReader类的Read方法依次读取网页源程序代码每一行的内容,直至行尾(读取的编码格式:UTF8)

//使用respStreamReader.Read來讀出0~256 字元

StreamReader respStreamReader = new StreamReader(respStream, Encoding.UTF8);
byteRead = respStreamReader.Read(cbuffer,0,256);

//一次一行把stream 內容抓出並存入strBuff裡,strBuff存了網頁的原始檔

while (byteRead != 0)
{
string strResp = new string(cbuffer,0,byteRead);
strBuff = strBuff + strResp;
byteRead = respStreamReader.Read(cbuffer,0,256);

 

}

//這邊取得網頁的title作為資料夾的名稱, strBuff.IndexOf("<title>")回傳找到第一個符合的第一個字元位置
docStart = strBuff.IndexOf("<title>");
docStop = docStart + 16;

 

// strBuff.Substring(docStart + 7, 15) 取出strBuff的子字串從docStart + 7往後算15個字元
document = strBuff.Substring(docStart + 7, 15);

//產生資料夾
System.IO.Directory.CreateDirectory("h:\\" + document);
respStream.Close();

//掃整個strBuff
while (temp < strBuff.Length)
{

//先抓出小圖路徑在原始檔的位置存到a
a = strBuff.IndexOf(HTTP,c);
if ((!first)&&(a == -1||a-c>500))
{
break;
}

//從a往後找到"A1/" 的位置,做為開始抓圖名的起點
b = strBuff.IndexOf(start, a + HTTP.Length);

 

//從b往後找到 " ,為檔名結尾

c = strBuff.IndexOf(end, b + start.Length);

//取出檔名
subS = strBuff.Substring(b, c - b-2 );

//檔名接上預設的路徑內容,即為大圖位置
subS = defString + subS;
label2.Text = subS;

 


//存到這個資料夾內
//System.IO.StreamWriter file = new System.IO.StreamWriter("h:\\test.txt");

saveName = "h:\\" + document +"\\"+ count + ".jpg";

//將圖下載並存到saveName,即完成
wc.DownloadFile(subS, saveName);
count++;
first = false;
}

//filename = strBuff;
// file.WriteLine(strBuff);
// file.Close();

MessageBox.Show("done!");
}

 

private void textBox1_MouseDown(object sender, MouseEventArgs e)
{

//點到輸入欄時,清除上次的,以方便使用
textBox1.Text = "";
}

 

private void button2_Click(object sender, EventArgs e)
{
FolderBrowserDialog path = new FolderBrowserDialog();

 

path.ShowDialog();

 

this.textBox2.Text = path.SelectedPath;

 

}

 未命名2  


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 alansong 的頭像
    alansong

    alansong的部落格

    alansong 發表在 痞客邦 留言(1) 人氣()