2010-01-19 39 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