简体   繁体   English

如何下载和解析 web 页面的一部分?

[英]How can i download and parse portion of web page?

I don't want to download the whole web page.我不想下载整个 web 页面。 It will take time and it needs lot of memory.这需要时间,需要大量 memory。

How can i download portion of that web page?我怎样才能下载那个 web 页面的一部分? Then i will parse that.然后我会解析它。

Suppose i need to download only the <div id="entryPageContent" class="cssBaseOne">...</div> .假设我只需要下载<div id="entryPageContent" class="cssBaseOne">...</div> How can i do that?我怎样才能做到这一点?

You can't download a portion of a URL by "only this piece of HTML".您不能通过“仅此一段 HTML”来下载 URL 的一部分。 HTTP only supports byte ranges for partial downloads and has no concept of HTML/XML document trees. HTTP 只支持字节范围的部分下载,没有 HTML/XML 文档树的概念。

So you'll have to download the entire page, load it into a DOM parser , and then extract only the portion(s) you need.因此,您必须下载整个页面,将其加载到DOM 解析器中,然后仅提取您需要的部分。

eg例如

$html = file_get_contents('http://example.com/somepage.html');
$dom = new DOM();
$dom->loadHTML($html);
$div = $dom->getElementById('entryPageContent');

$content = $div->saveHTML();

Using this:使用这个:

curl_setopt($ch, CURLOPT_RANGE, "0-10000");

will make cURL download only the first 10k bytes of the webpage.将使 cURL 仅下载网页的前 10k 字节。 Also it will only work if the server side supports this.此外,它仅在服务器端支持时才有效。 Many interpreted scripts (CGI, PHP, ...) ignore it.许多解释脚本(CGI,PHP,...)忽略它。

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

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