我正在进行屏幕抓取(screen scraping)的工作,想要获取特定页面的源代码。
如何使用JavaScript实现这一点?请帮帮我。
我正在进行屏幕抓取(screen scraping)的工作,想要获取特定页面的源代码。
如何使用JavaScript实现这一点?请帮帮我。
开始的简单方法,尝试使用jQuery
$("#links").load("/Main_Page #jq-p-Getting-Started li");
select * from html where url="http://stackoverflow.com"
将会为您提供一个JSON数组(我选择了这个选项),如下所示
"results": {
"body": {
"noscript": [
{
"div": {
"id": "noscript-padding"
}
},
{
"div": {
"id": "noscript-warning",
"p": "Stack Overflow works best with JavaScript enabled"
}
}
],
"div": [
{
"id": "notify-container"
},
{
"div": [
{
"id": "header",
"div": [
{
"id": "hlogo",
"a": {
"href": "/",
"img": {
"alt": "logo homepage",
"height": "70",
"src": "http://i.stackoverflow.com/Content/Img/stackoverflow-logo-250.png",
"width": "250"
}
……..
这样做的好处是你可以使用投影和where子句,从而将爬取到的数据结构化,并且只获取你需要的数据(最终通过网络传输的带宽更小)
例如:
select * from html where url="http://stackoverflow.com" and
xpath='//div/h3/a'
会让你获得
"results": {
"a": [
{
"href": "/questions/414690/iphone-simulator-port-for-windows-closed",
"title": "Duplicate: Is any Windows simulator available to test iPhone application? as a hobbyist who cannot afford a mac, i set up a toolchain kit locally on cygwin to compile objecti … ",
"content": "iphone\n simulator port for windows [closed]"
},
{
"href": "/questions/680867/how-to-redirect-the-web-page-in-flex-application",
"title": "I have a button control ....i need another web page to be redirected while clicking that button .... how to do that ? Thanks ",
"content": "How\n to redirect the web page in flex application ?"
},
…..
select title from html where url="http://stackoverflow.com" and
xpath='//div/h3/a'
"results": {
"a": [
{
"title": "I don't want the function to be entered simultaneously by multiple threads, neither do I want it to be entered again when it has not returned yet. Is there any approach to achieve … "
},
{
"title": "I'm certain I'm doing something really obviously stupid, but I've been trying to figure it out for a few hours now and nothing is jumping out at me. I'm using a ModelForm so I can … "
},
{
"title": "when i am going through my project in IE only its showing errors A runtime error has occurred Do you wish to debug? Line 768 Error:Expected')' Is this is regarding any script er … "
},
{
"title": "I have a java batch file consisting of 4 execution steps written for analyzing any Java application. In one of the steps, I'm adding few libs in classpath that are needed for my co … "
},
{
……
var titleList = $.getJSON(theAboveUrl);
带着它并开始玩吧。
漂亮,不是吗?
只要通过你的域上的代理抓取所需页面,就可以使用JavaScript:
<html>
<head>
<script src="/js/jquery-1.3.2.js"></script>
</head>
<body>
<script>
$.get("www.mydomain.com/?url=www.google.com", function(response) {
alert(response)
});
</script>
</body>
您可以简单地使用 XmlHttp
(AJAX)来访问所需的URL,URL返回的HTML响应将在responseText
属性中可用。如果不是同一域,您的用户将收到浏览器警报,显示类似于“此页面正在尝试访问不同的域。您是否要允许此操作?”的消息。
const URL = 'https://www.sap.com/belgique/index.html';
fetch(URL)
.then(res => res.text())
.then(text => {
console.log(text);
})
.catch(err => console.log(err));
如果你一定需要使用JavaScript,你可以使用ajax请求加载页面源代码。
请注意,使用JavaScript时,你只能检索请求页面所在域下的页面。
<html>
<head>
<script src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.js" ></script>
</head>
<body>
<script>
$.get("www.google.com", function(response) { alert(response) });
</script>
</body>
在你的Javascript代码中,你只需要像这样发送一个GET请求:
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
jsontext = request.responseText;
alert(jsontext);
}
request.open("GET", "https://extraction.import.io/query/extractor/THE_PUBLIC_LINK_THEY_GIVE_YOU?_apikey=YOUR_KEY&url=YOUR_URL", true);
request.send();
<html>
<head>
<base href='http://apod.nasa.gov/apod/'>
<title>test</title>
<style>
body { margin: 0 }
textarea { outline: none; padding: 2em; width: 100%; height: 100% }
</style>
</head>
<body onload="w=window.open('#'); x=document.getElementById('t'); a='<html>\n'; b='\n</html>'; setTimeout('x.innerHTML=a+w.document.documentElement.innerHTML+b; w.close()',2000)">
<textarea id=t></textarea>
</body>
</html>
TypeError: undefined is not an object (evaluating 'w.document')
。 - Quentin
$.ajax
来处理外部链接。这里有一个解决方案 - https://dev59.com/smbWa4cB1Zd3GeqPY6ns#18447625 - otaxige_aol