我正在尝试编写一个javascript,它将从BBC网站(http://www.bbc.co.uk/news)的头条新闻故事中抓取内部HTML代码,并将其放在txt文档中。我不太了解javascript,我更了解。bat和。vbs,但我知道他们不能这样做。
我不知道如何处理这个问题。我想让它扫描一个固定的外部html代码,然后复制内部的一个txt文件。
然而,我似乎找不到一个外层的html代码,是永久的每天。例如,这是今天的标题。
<span class="title-link__title-text">Benefit plan 'could hit young Britons'</span>
如你所见,它包含了标题。
我用的是火狐浏览器,如果这有什么不同的话。
任何帮助都将非常感激。
问候,
志片。
纯客户端浏览器方法:
好吧,我为你做了这个小提琴,也可以帮助别人。这对我来说很有趣,也很有挑战性。以下是我如何实现可能的解决方案的要点
- 使用ECMA 5 Blob Api动态创建文本文件
- 在iframe中加载http://www.bbc.co.uk/news (跨域原始访问-参见下面的注释部分)
- 在iframe加载事件触发超时使用setTimeout或setInterval (注释 - 根据您的需要每小时或每天重复执行) (根据您的需要调整时间)。
- 使用document.querySelectorAll("。Title-link span")似乎在审查网页源代码的基础上,使其具有一般性。
- 查看提琴手链接
(function () {
var textFile = null,
makeTextFile = function (text) {
var data = new Blob([text], {
type: 'text/plain'
});
// If we are replacing a previously generated file we need to
// manually revoke the object URL to avoid memory leaks.
if (textFile !== null) {
window.URL.revokeObjectURL(textFile);
}
textFile = window.URL.createObjectURL(data);
return textFile;
};
var iframe = document.getElementById('frame');
var commFunc = function () {
var iframe2 = document.getElementById('frame'); //This is required to get the fresh updated DOM
var innerDoc = iframe2.contentDocument || iframe2.contentWindow.document;
var getAll = Array.prototype.slice.call(innerDoc.querySelectorAll(".title-link span"));
var dummy = "";
for (var obj in getAll) {
dummy = dummy.concat("n" + (getAll[obj]).innerText);
}
var link = document.createElement("a");
link.href = makeTextFile(dummy);
link.download = "sample.txt"
link.click();
console.log("Downloaded the sample.txt file");
};
iframe.onload = function () {
setTimeout(commFunc, 1000); //Adjust the time required to load
//setInterval(commFunc, 1000);
};
//Click the button when the page inside the iframe is loaded
create.addEventListener('click', commFunc);
})();
HTML:<span class="title-link__title-text">Benefit plan 'could hit young Britons'</span>
<div>
<iframe id="frame" src="http://www.bbc.co.uk/news"></iframe>
</div>
<button id="create">Download</button>
注意:
- 要在chrome上运行上述javascript,您需要禁用web安全。上面的脚本应该在firefox上运行良好,不需要任何调整。
- 这是一个可能的插图,可以实现使用纯浏览器脚本。
- 针对现代浏览器
建议方法:
使用node.js服务器,你可以修改上面的脚本为运行stanalone
或任何服务器端脚本框架,如php, java spring等
使用Node js方法:
Javascript:
var jsdom = require("node-jsdom");
var fs = require("fs");
jsdom.env({
url: "http://www.bbc.co.uk/news",
scripts: ["http://code.jquery.com/jquery.js"],
done: function (errors, window) {
var $ = window.$;
console.log("HN Links");
$(".title-link span").each(function() {
//console.log(" -", $(this).text());
fs.existsSync("sample.txt") === true ? fs.appendFile("sample.txt", "r"+ $(this).text()) : fs.writeFile("sample.txt", "r"+ $(this).text())
});
}
});
以上代码的依赖项:
- NodeJS
- JSDOM Jquery
- <
- NodeJS文件系统/gh>
希望它对你和其他人也有帮助
My thoughts -
JS可以用来从页面中获取数据/文本,但是,要将其保存到文件中,你必须在后台使用Python或PHP等,
为什么使用JS?使用CURL可以很好地抓取网页。
你可以使用-
抓取/下载网页<?php
// Defining the basic cURL function
function curl($url) {
$ch = curl_init(); // Initialising cURL
curl_setopt($ch, CURLOPT_URL, $url); // Setting cURL's URL option with the $url variable passed into the function
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // Setting cURL's option to return the webpage data
$data = curl_exec($ch); // Executing the cURL request and assigning the returned data to the $data variable
curl_close($ch); // Closing cURL
return $data; // Returning the data from the function
}
?>
然后随意使用该函数-
<?php
$scraped_website = curl("http://www.yahoo.com"); // Executing our curl function to scrape the webpage http://www.yahoo.com and return the results into the $scraped_website variable
?>
—— 参考链接
使用PHP和CURL抓取网页
使用CURL在PHP中抓取
可以使用HTML元素的DIV和Node来更清楚地抓取。看看这些-第1部分-第2部分-第3部分
希望有帮助。编码快乐!
您想从html下载内容的txt文件?如果你想从所有的标题栏中获取文本,你需要这样做
var txt = "";
var nodeList = document.querySelectorAll(".title-link__title-text")
for(var i=0; i<nodeList.length;i++){
txt+="n"+nodeList[i].innerText;
}
然后将txt变量写入文件,就像上面提到的那样