Javascript 用于在网站上检查关键字的脚本
我想写一个脚本,通过url的检查列表检查它们是否有效 页面不会重定向到404,而是显示“对不起,找不到!”!如果URL无效 因此,如果脚本找到这句话,URL是无效的。如果没有,则很可能是有效的 你知道如何在JS中实现这一点吗?也欢迎指向其他语言中可能的方法的指针Javascript 用于在网站上检查关键字的脚本,javascript,python,validation,keyword,Javascript,Python,Validation,Keyword,我想写一个脚本,通过url的检查列表检查它们是否有效 页面不会重定向到404,而是显示“对不起,找不到!”!如果URL无效 因此,如果脚本找到这句话,URL是无效的。如果没有,则很可能是有效的 你知道如何在JS中实现这一点吗?也欢迎指向其他语言中可能的方法的指针 谢谢 一种简单的Python方法是: import requests urls = ['https://www.google.com'] # Fill this however for url in urls: resp =
谢谢 一种简单的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
我成功地使用了jQuery。我不认为任何人可以单独用javascript实现这一点。无论如何,您都必须使用jQuery 首先,您应该在Chrome控制台中试用: 1.添加此扩展以消除CORS策略错误 . 确保它在Chrome->More Tools->Extensions中启用 2.现在我们必须运行get(),不能像在.js文件中通常使用的$.get()那样调用它。因此,我们需要通过在console中运行以下行将其转换为console:
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.消防请求:
var rsp = jQuery.get("https://www.google.com/");
等待2秒…(ES6已禁用同步请求,因此等待rsp填充)
尝试无效的url:
var rsp = jQuery.get("https://www.goesfsfsfsffogle.com/");
等2秒钟
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项目文件中运行:
同样,如果rsp不包含readyState==4,则表示尚未收到异步响应。在这种情况下,我们需要在if check之前添加wait
如果这对您没有帮助,请告诉我。到目前为止您尝试了什么方法?你的密码在哪里?你的密码在哪里?请张贴您的代码,以便我可以尝试帮助每次我需要检查网址,搜索网页等。。。我将Ruby与
Mechanize
gem一起使用。太棒了。我试着用curl来搞乱bash脚本。由于这不起作用,我实际上只是在寻找输入。我不希望有人为我提供代码,只是善意地告诉我实现目标的可能方法。不用担心,很乐意提供帮助
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");
}
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");
}
}