最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Grab a website with PHP and then traverse it with jQuery - Stack Overflow

programmeradmin1浏览0评论

I'm building a system where I'll need to grab the contents of a web page with PHP and then parse it to extract certain tables etc. Is there an easy way to do this with jQuery or would the best way be to write PHP function to extract the data?

I'm building a system where I'll need to grab the contents of a web page with PHP and then parse it to extract certain tables etc. Is there an easy way to do this with jQuery or would the best way be to write PHP function to extract the data?

Share Improve this question asked Nov 3, 2010 at 19:58 greenimpalagreenimpala 3,9763 gold badges32 silver badges39 bronze badges 1
  • This may help, too: stackoverflow./questions/292926 – mdo Commented Nov 3, 2010 at 20:54
Add a ment  | 

6 Answers 6

Reset to default 7

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()
    }

You can use the DOM functions available in PHP http://php/manual/en/book.dom.php

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/projects/simplehtmldom/

example:

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

// 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.

发布评论

评论列表(0)

  1. 暂无评论