【问题标题】:Download an Entire Website in C#用 C# 下载整个网站
【发布时间】:2011-01-06 16:46:14
【问题描述】:

请原谅我对这个问题的无知

我正在使用

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

下载网页。你能帮我增强代码以便下载整个网站吗?尝试使用 HTML 屏幕抓取,但它只返回 index.html 文件的 href 链接。我如何继续前进

谢谢

【问题讨论】:

  • 您的问题解决了吗?

标签: c# web screen-scraping screen download


【解决方案1】:

抓取网站实际上是一项繁重的工作,其中有很多极端案例。

改为调用wgetmanual 解释了如何使用“recursive retrieval”选项。

【讨论】:

    【解决方案2】:
     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;
        }
    

    【讨论】:

    • +1,还记得解析所有文本文件(html、css),因为它们可以链接到其他资源
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-12
    • 2018-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多