代码之家  ›  专栏  ›  技术社区  ›  Constantin Schreiber

用于检查网站上关键字的脚本

  •  0
  • Constantin Schreiber  · 技术社区  · 7 年前

    我想写一个脚本,通过一个url列表检查它们是否有效。

    你知道如何在JS中实现这一点吗?也欢迎指向其他语言中可能的方法的指针!

    谢谢

    2 回复  |  直到 7 年前
        1
  •  0
  •   dheiberg    7 年前

    一种简单的Python方法是:

    import requests
    
    urls = ['https://www.google.com'] # Fill this however
    for url in urls:
        resp = requests.get(url)
        if 'Sorry, not found!' in resp.text:
            print(url + ' had no page') # or something
    
        2
  •  0
  •   user3206070    7 年前

    jQuery .我认为没有人能单独用javascript做到这一点。无论如何,您都必须使用jQuery。

    首先,您应该在Chrome控制台中试用:

    1.添加此扩展以消除CORS策略错误 Chrome Extension 。确保在Chrome中启用它->更多工具->扩展

    2.现在我们必须运行get(),不能像$那样调用它。get(),通常在.js文件中使用。因此,我们需要通过在控制台中运行以下行将其转换为控制台:

    var jq = document.createElement('script');
    jq.src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js";
    document.getElementsByTagName('head')[0].appendChild(jq);
    

    3.Fire get请求:

    var rsp = jQuery.get("https://www.google.com/");
    

    if (rsp.responseText && rsp.responseText.includes("was not found")) { //In your js file replace with Sorry! not found
    console.log("The Url is Invalid"); 
    }
    else {
    console.log("could be a valid url"); //this must get printed
    }
    

    尝试无效的url:

    var rsp = jQuery.get("https://www.goesfsfsfsffogle.com/");
    

    if (rsp.responseText && rsp.responseText.includes("was not found")) { //In your js file replace with Sorry! not found
    console.log("The Url is Invalid"); //this must get printed
    }
    else {
    console.log("could be a valid url"); 
    }
    

    在jQuery项目文件中运行:

    var urls = ["https://www.google.com/"];
    var url;
    for ( url in urls ){
    var rsp = $.get(url);
    //A wait should be added here for rsp to get populated
    //console.log("readyState="+rsp.readyState);
    if (rsp.responseText && rsp.responseText.includes("Sorry! not found")) 
    {  
    console.log("The Url is Invalid"); 
    }
    else {
    console.log("Its a valid url"); 
    }
    }
    

    同样,如果rsp不包含readyState==4,则表示尚未收到异步响应。在这种情况下,我们需要在if检查之前添加等待。