我想从网页中获取div内容,并在我的页面中使用它.
I want to fetch div content from a webpage and to use it in my page.
我的网址为 www.freebase /search?limit = 30& start = 0& query = cancer 我想获取ID为artilce-1001的div内容.如何在php或jQuery中做到这一点?
I have the url www.freebase/search?limit=30&start=0&query=cancer I want to fetch div content with id artilce-1001. How can I do that in php or jQuery?
推荐答案如果要使用PHP,则可能需要查看简单的HTML DOM .这是一个不错的单个包含文件. 文档给出了一个将斜杠刮取为以下示例:
If you want to use PHP, you may want to have a look at Simple HTML DOM. It is a nice single include file. The docs give an example of scraping slashdot as:
$html = file_get_html('slashdot/'); // Find all article blocks foreach($html->find('div.article') as $article) { $item['title'] = $article->find('div.title', 0)->plaintext; $item['intro'] = $article->find('div.intro', 0)->plaintext; $item['details'] = $article->find('div.details', 0)->plaintext; $articles[] = $item; }Regex永远都不擅长(并且永远不应该用于)解析HTML.它不是正规的,您最终会得到大量的正规表达式,以表示在jQuery或上述库中简单的内容
Regex is never any good at (and should never be used for) parsing HTML. It isn't regular, and you end up with huge regular expressions for what would be simple in jQuery or the above library
所以你想使用类似的东西
So you would want to use something like
$html = file_get_html('www.freebase/search?limit=30&start=0&query=cancer'); $text = $html->find('div[id=artilce-1001]',0)->plaintext;更多推荐
如何从网页中获取内容?
发布评论