Selenium构建了404s的列表

时间:2012-10-13 05:00:14

标签: selenium web-scraping

是否可以让Selenium抓取TLD并逐步导出任何404的列表?

我被困在Windows机器上几个小时,想要在回到* nix的舒适之前进行一些测试......

1 个答案:

答案 0 :(得分:1)

我不太了解Python,也不了解任何常用的库,但我可能会做这样的事情(使用C#代码作为示例,但概念应该适用):

// WARNING! Untested code here. May not completely work, and
// is not guaranteed to even compile.

// Assume "driver" is a validly instantiated WebDriver instance
// (browser used is irrelevant). This API is driver.get in Python,
// I think.
driver.Url = "http://my.top.level.domain/";

// Get all the links on the page and loop through them,
// grabbing the href attribute of each link along the way.
// (Python would be driver.find_elements_by_tag_name)
List<string> linkUrls = new List<string>();
ReadOnlyCollection<IWebElement> links = driver.FindElement(By.TagName("a"));
foreach(IWebElement link in links)
{
    // Nice side effect of getting the href attribute using GetAttribute()
    // is that it returns the full URL, not relative ones.
    linkUrls.Add(link.GetAttribute("href"));
}

// Now that we have all of the link hrefs, we can test to
// see if they're valid.
List<string> validUrls = new List<string>();
List<string> invalidUrls = new List<string>();
foreach(string linkUrl in linkUrls)
{
    HttpWebRequest request = WebRequest.Create(linkUrl) as HttpWebRequest;
    request.Method = "GET";

    // For actual .NET code, you'd probably want to wrap this in a
    // try-catch, and use a null check, in case GetResponse() throws,
    // or returns a type other than HttpWebResponse. For Python, you
    // would use whatever HTTP request library is common.

    // Note also that this is an extremely naive algorithm for determining
    // validity. You could just as easily check for the NotFound (404)
    // status code.
    HttpWebResponse response = request.GetResponse() as HttpWebResponse;
    if (response.StatusCode == HttpStatusCode.OK)
    {
        validUrls.Add(linkUrl);
    }
    else
    {
        invalidUrls.Add(linkUrl);
    }
}

foreach(string invalidUrl in invalidUrls)
{
    // Here is where you'd log out your invalid URLs
}

此时,您有一个有效和无效网址列表。您可以将这一切包装成一个可以将TLD URL传递到其中的方法,并使用每个有效的URL递归调用它。这里的关键是你没有使用Selenium来实际确定链接的有效性。如果您真的正在进行递归爬行,那么您不希望“点击”链接以导航到下一页。相反,您需要直接导航到页面上的链接。

您可能采取其他方法,例如通过代理运行所有内容,以及通过这种方式捕获响应代码。这取决于您希望如何构建解决方案。