简体   繁体   中英

Grab a website with PHP and then traverse it with jQuery

我正在构建一个系统,我需要用PHP抓取网页的内容,然后解析它以提取某些表等。有一个简单的方法用jQuery做这个或者最好的方法是编写PHP提取数据的功能?

jQuery has nothing to do with PHP and can't be run without a browser, so you're out of luck there.

However, there is phpQuery that allows DOM parsing with jQuery's selectors!

Do It like this in php with native php DOM functions and xpath:

    $dom = new DOMDocument();
    @$dom->loadHTML($html);
    $x = new DOMXPath($dom);
    // grab all tables with id of foo
    foreach($x->query("//table[@id='foo']") as $node)
    {
        // here is the html
                    echo $node->c14n();
                    // grab the containing text 
                    echo $node->textContent()
    }

您可以使用PHP http://php.net/manual/en/book.dom.php中提供的DOM函数

You can't. jQuery is for JavaScript, which is client-side, and requires a JavaScript engine to execute.

I would suggest you read the HTML as XML, but you'll run into all sorts of trouble if the HTML is not XHTML valid.

this is awesome

http://sourceforge.net/projects/simplehtmldom/

example:

// Create DOM from URL or file
$html = file_get_html('http://www.google.com/');

// Find all images
foreach($html->find('img') as $element)
       echo $element->src . '<br>';

// Find all links
foreach($html->find('a') as $element)
       echo $element->href . '<br>'; 

There are a few php packages that can help you with this, curl, dom and xpath.

Here's a good tutorial I've used before.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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