2010-01-19 89 views
5

原諒的主題下載整個網站在C#

我的無知,我使用

string p="http://" + Textbox2.text; 
string r= textBox3.Text; 
System.Net.WebClient webclient=new 
System.Net.Webclient(); 
webclient.DownloadFile(p,r); 

下載的網頁。你能幫我加強代碼,以便下載整個網站。嘗試使用HTML Screen Scraping,但它只返回index.html文件的href鏈接。我如何着手未來的日子

感謝

+0

您是否解決了問題? – Jason 2010-01-22 21:19:46

回答

8
protected string GetWebString(string url) 
    { 
     string appURL = url; 
     HttpWebRequest wrWebRequest = WebRequest.Create(appURL) as HttpWebRequest; 
     HttpWebResponse hwrWebResponse = (HttpWebResponse)wrWebRequest.GetResponse(); 

     StreamReader srResponseReader = new StreamReader(hwrWebResponse.GetResponseStream()); 
     string strResponseData = srResponseReader.ReadToEnd(); 
     srResponseReader.Close(); 
     return strResponseData; 
    } 

這會將網頁從提供的URL中放入字符串中。

然後,您可以使用REGEX來解析字符串。

這個小塊從craigslist中獲取特定鏈接並將它們添加到arraylist中...修改爲您的目的。

protected ArrayList GetListings(int pages) 
    { 
      ArrayList list = new ArrayList(); 
      string page = GetWebString("http://albany.craigslist.org/bik/"); 

      MatchCollection listingMatches = Regex.Matches(page, "(<p><a href=\")(?<LINK>/.+/.+[.]html)(\">)(?<TITLE>.*)(-</a>)"); 
      foreach (Match m in listingMatches) 
      { 
       list.Add("http://albany.craigslist.org" + m.Groups["LINK"].Value.ToString()); 
      } 
      return list; 
    } 
+0

+1,還記得解析所有的文本文件(html,css),因爲它們可以鏈接到其他資源 – 2010-01-19 08:41:53