2017-10-01 3 views
-1

다음 코드를 사용하여 웹 페이지의 모든 이미지 URL 목록을 가져옵니다. 대상은 Google 이미지 검색에서 모든 이미지 URL을 가져 오는 것입니다. 내 문제는 내가 20 URL과 URL의 사진을 얻을 수있는 작은 사진과 실제 크기가 없습니다.Google 이미지에서 URL 가져 오기

public List<string> FetchImages(string Url) 
{ 
    List<string> imageList = new List<string>(); 

    //Append http:// if necessary 
    if (!Url.StartsWith("http://") && !Url.StartsWith("https://")) 
     Url = "http://" + Url; 

    string responseUrl = string.Empty; 
    string htmlData = ASCIIEncoding.ASCII.GetString(DownloadData(Url, out responseUrl)); 

    if (responseUrl != string.Empty) 
     Url = responseUrl; 

    if (htmlData != string.Empty) 
    { 
     string imageHtmlCode = "<img"; 
     string imageSrcCode = @"src="""; 

     int index = htmlData.IndexOf(imageHtmlCode); 
     while (index != -1) 
     { 
      //Remove previous data 
      htmlData = htmlData.Substring(index); 

      //Find the location of the two quotes that mark the image's location 
      int brackedEnd = htmlData.IndexOf('>'); //make sure data will be inside img tag 
      int start = htmlData.IndexOf(imageSrcCode) + imageSrcCode.Length; 
      int end = htmlData.IndexOf('"', start + 1); 

      //Extract the line 
      if (end > start && start < brackedEnd) 
      { 
       string loc = htmlData.Substring(start, end - start); 

       //Store line 
       imageList.Add(loc); 
      } 

      //Move index to next image location 
      if (imageHtmlCode.Length < htmlData.Length) 
       index = htmlData.IndexOf(imageHtmlCode, imageHtmlCode.Length); 
      else 
       index = -1; 
     } 

     //Format the image URLs 
     for (int i = 0; i < imageList.Count; i++) 
     { 
      string img = imageList[i]; 

      string baseUrl = GetBaseURL(Url); 

      if ((!img.StartsWith("http://") && !img.StartsWith("https://")) 
       && baseUrl != string.Empty) 
       img = baseUrl + "/" + img.TrimStart('/'); 

      imageList[i] = img; 
     } 
    } 

    return imageList; 
} 
private string GetBaseURL(string Url) 
{ 
    int inx = Url.IndexOf("://") + "://".Length; 
    int end = Url.IndexOf('/', inx); 

    string baseUrl = string.Empty; 
    if (end != -1) 
     return Url.Substring(0, end); 
    else 
     return string.Empty; 
} 
private byte[] DownloadData(string Url, out string responseUrl) 
{ 
    byte[] downloadedData = new byte[0]; 
    try 
    { 
     //Get a data stream from the url 
     WebRequest req = WebRequest.Create(Url); 
     WebResponse response = req.GetResponse(); 
     Stream stream = response.GetResponseStream(); 

     responseUrl = response.ResponseUri.ToString(); 

     //Download in chuncks 
     byte[] buffer = new byte[1024]; 

     //Get Total Size 
     int dataLength = (int)response.ContentLength; 

     //Download to memory 
     //Note: adjust the streams here to download directly to the hard drive 
     MemoryStream memStream = new MemoryStream(); 
     while (true) 
     { 
      //Try to read the data 
      int bytesRead = stream.Read(buffer, 0, buffer.Length); 

      if (bytesRead == 0) 
      { 
       break; 
      } 
      else 
      { 
       //Write the downloaded data 
       memStream.Write(buffer, 0, bytesRead); 
      } 
     } 

     //Convert the downloaded stream to a byte array 
     downloadedData = memStream.ToArray(); 

     //Clean up 
     stream.Close(); 
     memStream.Close(); 
    } 
    catch (Exception) 
    { 
     responseUrl = string.Empty; 
     return new byte[0]; 
    } 

    return downloadedData; 
    } 
+1

나는 왜 이미지가 20 장 밖에 안되는지는 모르겠지만 작은 이미지를 얻는 방법에 대한 답을 얻었습니다. 실제로는 페이지로드시 물리적으로 보이지 않습니다. 페이지의 요소를 검사하면 모든 페이지가 자바 스크립트로 완벽하게 처리되고 동적으로 이미지 및 URL이 사용자 상호 작용에로드된다는 것을 알 수 있습니다. 로드 후 일반 페이지에는 실제로 작은 이미지 만 포함됩니다. – Zorak

+0

나는 이해하지 못한다. Google이나 다른 사이트에서 이미지를 스크랩하고 있습니까? –

+0

Google에서 .. 누군가 다른 이미지 검색 엔진을 알고 있을지도 몰라? –

답변

0

구글은 한 번에 이미지의 일정 금액을로드하는 것 같다

여기에 코드입니다. 컴퓨터를 스크롤하면 더 많이로드됩니다. 채워지지 않은 이미지에 도달하면 이미지의 끝으로 간주 할 수 있습니까?

관련 문제