简体   繁体   English

用js抓取html

[英]Scrape html with js

I'm trying to get the html of www.soccerway.com . 我正在尝试获取www.soccerway.com的html。 In particular this: 特别是:

在此处输入图片说明

that have the label-wrapper class I also tried with: select.nav-select but I can't get any content. 那些我也尝试过的label-wrapper类: select.nav-select但我什么也没得到。 What I did is: 我所做的是:

1) Created a php filed called grabber.php , this file have this code: 1)创建了一个名为grabber.php的php文件,该文件具有以下代码:

<?php echo file_get_contents($_GET['url']); ?>

2) Created a index.html file with this content: 2)创建具有以下内容的index.html文件:

<!DOCTYPE html>
<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    <meta charset=utf-8 />
    <title>test</title>
</head>
<body>

<div id="response"></div>

</body>

<script>
    $(function(){
        var contentURI= 'http://soccerway.com';    
        $('#response').load('grabber.php?url='+ encodeURIComponent(contentURI) + ' #label-wrapper');
    });
    var LI = document.querySelectorAll(".list li");
    var result = {};

    for(var i=0; i<LI.length; i++){
        var el = LI[i];
        var elData = el.dataset.value;
        if(elData) result[el.innerHTML] = elData; // Only if element has data-value attr
    }

    console.log( result );
</script>

</html>

in the div there is no content grabbed, I tested my js code for get all the link and working but I've inserted the html page manually . 在div中没有​​内容被抓取,我测试了我的js代码以获取所有链接并正常工作,但是我手动插入了html页面。

I see a couple issues here. 我在这里看到几个问题。

var contentURI= 'http:/soccerway.com #label-wrapper';

You're missing the second slash in http:// , and you're passing a URL with a space and an ID to file_get_contents . 您在http://缺少第二个斜杠,并且正在将带有空格和ID的URL传递给file_get_contents You'll want this instead: 您将改为:

var contentURI = 'http://soccerway.com/';

and then you'll need to parse out the item you're interested in from the resulting HTML. 然后您需要从结果HTML中解析出您感兴趣的项目。

The #label-wrapper needs to be in the jQuery load() call, not the file_get_contents , and the contentURI variable needs to be properly escaped with encodeURIComponent : #label-wrapper必须在jQuery load()调用中,而不是在file_get_contents ,并且contentURI变量需要使用encodeURIComponent进行正确的转义:

$('#response').load('grabber.php?url='+ encodeURIComponent(contentURI) + ' #label-wrapper');

Your code also contains a massive vulnerability that's potentially very dangerous, as it allows anyone to access grabber.php with a url value that's a file location on your server. 您的代码还包含一个巨大的漏洞,它可能非常危险,因为它允许任何人使用url值(即服务器上的文件位置)访问grabber.php This could compromise your database password or other sensitive data on the server. 这可能会损害您的数据库密码或服务器上的其他敏感数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM