如何使用 Javascript 执行客户端网页抓取?
How do I perform clientside web-scraping with Javascript?
如何在不违反 same-origin policy? I have tried, unsuccessfully, to use a website like http://anyorigin.com/ 的情况下完成此操作,但它不起作用。我该如何做到这一点?
编辑 4/1/20 - 修复非功能代码:
嗯,事实证明 YQL 不再起作用了,但我们不会让它阻止我们。
事实证明,有一个名为 CloudQuery 的免费服务,我们可以使用它很容易地完成同样的事情。不幸的是,您在一段时间内可以拨打的电话数量有一个非常低的限制。
var myUrl = "https://cloudquery.t9t.io/query?url=https%3A%2F%2Fwhosebug.com%2Fusers%2F5732397%2Fstardustgogeta&selectors=*:nth-child(2)%20%3E%20*:nth-child(1)%20%3E%20*:nth-child(1)%20%3E%20*:nth-child(1)%20%3E%20*:nth-child(1)%20%3E%20*:nth-child(2)%20%3E%20*%20%3E%20*:nth-child(1)";
fetch(myUrl).then(r => r.json()).then(r => {document.write(r.contents[0].innerText)});
旧答案:
事实证明,雅虎 YQL (Yahoo Query Language) can use XPath 可以在线查找页面 HTML 的元素。
只需在您的文档中包含以下内容:
<div id="a"></div>
<script>
var yqlCallback = function(data){
var rep = data.query.results.div;
document.getElementById('a').innerHTML = "StardustGogeta's reputation is "+rep+'.';
};
</script>
<script type='application/javascript' src="https://query.yahooapis.com/v1/public/yql?q=select%20content%20from%20html%20where%20url%3D'http%3A%2F%2Fwhosebug.com%2Fusers%2F5732397%2Fstardustgogeta'%20and%20xpath%3D'%2F%2Fdiv%5B%40class%3D%22reputation%22%5D'&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=yqlCallback"></script>
如何在不违反 same-origin policy? I have tried, unsuccessfully, to use a website like http://anyorigin.com/ 的情况下完成此操作,但它不起作用。我该如何做到这一点?
编辑 4/1/20 - 修复非功能代码:
嗯,事实证明 YQL 不再起作用了,但我们不会让它阻止我们。
事实证明,有一个名为 CloudQuery 的免费服务,我们可以使用它很容易地完成同样的事情。不幸的是,您在一段时间内可以拨打的电话数量有一个非常低的限制。
var myUrl = "https://cloudquery.t9t.io/query?url=https%3A%2F%2Fwhosebug.com%2Fusers%2F5732397%2Fstardustgogeta&selectors=*:nth-child(2)%20%3E%20*:nth-child(1)%20%3E%20*:nth-child(1)%20%3E%20*:nth-child(1)%20%3E%20*:nth-child(1)%20%3E%20*:nth-child(2)%20%3E%20*%20%3E%20*:nth-child(1)";
fetch(myUrl).then(r => r.json()).then(r => {document.write(r.contents[0].innerText)});
旧答案:
事实证明,雅虎 YQL (Yahoo Query Language) can use XPath 可以在线查找页面 HTML 的元素。
只需在您的文档中包含以下内容:
<div id="a"></div>
<script>
var yqlCallback = function(data){
var rep = data.query.results.div;
document.getElementById('a').innerHTML = "StardustGogeta's reputation is "+rep+'.';
};
</script>
<script type='application/javascript' src="https://query.yahooapis.com/v1/public/yql?q=select%20content%20from%20html%20where%20url%3D'http%3A%2F%2Fwhosebug.com%2Fusers%2F5732397%2Fstardustgogeta'%20and%20xpath%3D'%2F%2Fdiv%5B%40class%3D%22reputation%22%5D'&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=yqlCallback"></script>