如何从网页中获取内容?

编程入门 行业动态 更新时间:2024-10-14 22:18:43
本文介绍了如何从网页中获取内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想从网页中获取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;

更多推荐

如何从网页中获取内容?

本文发布于:2023-11-25 23:08:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631715.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网页   内容

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!