检查网站关键字的脚本
Script to check for keyword on website
我想编写一个脚本,通过 url 的列表来检查它们是否有效。
该页面不会重定向到 404,而是显示“抱歉,找不到!”这句话。如果 URL 无效。
所以如果脚本找到这句话,URL是无效的。如果不是,则它很可能是有效的。
知道如何在 JS 中实现吗?也欢迎指出其他语言的可能方法!
谢谢!
一个简单的 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。
First you should try out in Chrome Console:
1.Add这个扩展去掉了CORS策略错误
Chrome Extension。确保在 Chrome->更多工具->扩展
中启用
2.Now 我们必须 运行 get() 并且我们不能像通常在 .js 文件中使用的 $.get() 那样调用它。因此,我们需要通过 运行 在控制台的以下行中将其转换为控制台:
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 获取请求:
var rsp = jQuery.get("https://www.google.com/");
等待 2 秒...(ES6 已禁用同步请求,因此请等待 rsp 填充)
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/");
等待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");
}
Running inside your jQuery project file:
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,则表示尚未收到异步响应。在这种情况下,我们需要在检查之前添加等待。
如果这对您没有帮助,请告诉我。
我想编写一个脚本,通过 url 的列表来检查它们是否有效。
该页面不会重定向到 404,而是显示“抱歉,找不到!”这句话。如果 URL 无效。
所以如果脚本找到这句话,URL是无效的。如果不是,则它很可能是有效的。
知道如何在 JS 中实现吗?也欢迎指出其他语言的可能方法!
谢谢!
一个简单的 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。
First you should try out in Chrome Console:
1.Add这个扩展去掉了CORS策略错误 Chrome Extension。确保在 Chrome->更多工具->扩展
中启用2.Now 我们必须 运行 get() 并且我们不能像通常在 .js 文件中使用的 $.get() 那样调用它。因此,我们需要通过 运行 在控制台的以下行中将其转换为控制台:
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 获取请求:
var rsp = jQuery.get("https://www.google.com/");
等待 2 秒...(ES6 已禁用同步请求,因此请等待 rsp 填充)
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/");
等待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");
}
Running inside your jQuery project file:
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,则表示尚未收到异步响应。在这种情况下,我们需要在检查之前添加等待。
如果这对您没有帮助,请告诉我。