我想自动从一个页面抓取一些内容。
我想知道是否有可能:
getelementbyid或类似的方法获取下一页的链接。典型的方法是使用LWP或使用CURL等PHP脚本编写Perl脚本,但这都是服务器端的。我想知道我能不能做好客户方面的工作。
发布于 2012-08-28 22:44:51
实际上,我做了一些类似的事情。
通过使用GreaseMonkey,您可以编写一个用户脚本,它将与所需的页面交互。你可以得到下一个页面链接并滚动你喜欢的东西。
您也可以在本地存储任何数据,通过一些名为GM_getValue和GM_setValue的新函数在火狐中存储。
我走懒散的路。我只生成一个很长的URL列表,我在浏览页面时找到了这些URL。我做了一个粗略的"document.write“方法,并将URL列表作为一个批处理文件在wget上进行规则。
此时,我复制并粘贴批处理文件,然后运行它。
如果您需要运行足够多的时间以使其自动化,过去有一种方法可以将GreaseMonkey脚本转换为火狐扩展,这些扩展可以获得更多的功能。
另一个选择是AFAIK,只有Chrome。您可以收集所需的任何信息,并从其中构建一个大文件,然后使用链接的download属性,然后进行一次单击来保存内容。
更新
我本来要分享我所做的全部代码,但是它与一个特定的网站联系得太紧密了,所以它不会有什么帮助--所以我会去寻找一个更“通用”的解决方案。
警告,此代码动态输入,可能实际上不正确。
// Define the container
// If you are crawling multiple pages, you'd want to load this from
// localStorage.
var savedLinks = [];
// Walk through the document and build the links.
for (var i = 0; i < document.links.length; i++) {
var link = document.links[i];
var data = {
url: link.url,
desc = getText(link)
};
savedLinks.push(data);
}
// Here you'd want to save your data via localStorage.
// If not on the last page, find the 'next' button and load the next page
// [load next page here]
// If we *are* on the last page, use document.write to output our list.
//
// Note: document.write totally destroys the current document. It really is quite
// an ugly way to do it, but in this case it works.
document.write(JSON.stringify(savedLinks, null, 2));发布于 2012-08-28 22:45:43
Selenium/webdriver将允许您编写一个简单的java/ruby/php应用程序,该应用程序将启动Firefox,使用其JavaScript引擎与浏览中的页面进行交互。
或者,如果网页不需要JavaScript使您看到感兴趣的内容可用,您可以使用您喜爱的语言的html解析器,并将浏览器排除在外。
如果你想在火狐的JavaScript中做这件事,你可能可以用一个很好的脚本来做。
https://stackoverflow.com/questions/12168417
复制相似问题