首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >抓取网页内容

问抓取网页内容
EN

Stack Overflow用户
提问于 2009-02-25 05:37:41
回答 4查看 71.2K关注 0票数 19

我正在开发一个项目,我想在后台抓取一个网站的内容,并从那个抓取的网站获得一些有限的内容。例如,在我的页面中,我有"userid“和"password”字段,通过它们,我可以访问我的邮件,抓取我的收件箱内容,并将其显示在我的页面中。

我通过单独使用javascript完成了上面的工作。但是,当我单击登录按钮时,我的页面(http://localhost/web/Login.html)的URL将更改为URL (http://mail.in.com/mails/inbox.php?nomail=...)。我就是被刮掉的。但是我没有更改我的url就删除了详细信息。

EN

回答 4

Stack Overflow用户

发布于 2009-02-27 11:18:09

一定要用PHP Simple HTML DOM Parser。这是快速,容易和超级灵活。它基本上将整个HTML页面放在一个对象中,然后您就可以访问该对象中的任何元素。

就像官方网站的例子一样,要获取Google主页上的所有链接:

代码语言:javascript
复制
// 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>';
票数 40
EN

Stack Overflow用户

发布于 2016-02-26 00:46:14

HTTP请求

首先,发出HTTP请求以获取页面内容。有几种方法可以做到这一点。

fopen

发送超文本传输协议请求的最基本方法是使用fopen。一个主要的优点是,您可以设置一次读取多少个字符,这在读取非常大的文件时非常有用。然而,这并不是做正确的最容易的事情,除非你正在读取非常大的文件并且害怕遇到内存问题,否则不建议这样做。

代码语言:javascript
复制
$fp = fopen("http://www.4wtech.com/csp/web/Employee/Login.csp", "rb");
if (FALSE === $fp) {
    exit("Failed to open stream to URL");
}

$result = '';

while (!feof($fp)) {
    $result .= fread($fp, 8192);
}
fclose($fp);
echo $result;

file_get_contents

最简单的方法就是使用file_get_contents。If的功能与fopen大致相同,但可供选择的选项更少。这里的一个主要优点是它只需要一行代码。

代码语言:javascript
复制
$result = file_get_contents('http://www.4wtech.com/csp/web/Employee/Login.csp');
echo $result;

套接字

如果您需要更多地控制发送到服务器的标头,您可以使用套接字,并与fopen结合使用。

代码语言:javascript
复制
$fp = fsockopen("www.4wtech.com/csp/web/Employee/Login.csp", 80, $errno, $errstr, 30);
if (!$fp) {
    $result = "$errstr ($errno)<br />\n";
} else {
    $result = '';
    $out = "GET / HTTP/1.1\r\n";
    $out .= "Host: www.4wtech.com/csp/web/Employee/Login.csp\r\n";
    $out .= "Connection: Close\r\n\r\n";
    fwrite($fp, $out);
    while (!feof($fp)) {
        $result .= fgets($fp, 128);
    }
    fclose($fp);
}
echo $result;

流

或者,您也可以使用streams。Streams类似于套接字,可以与fopen和file_get_contents结合使用。

代码语言:javascript
复制
$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Accept-language: en\r\n" .
              "Cookie: foo=bar\r\n"
  )
);

$context = stream_context_create($opts);

$result = file_get_contents('http://www.4wtech.com/csp/web/Employee/Login.csp', false, $context);
echo result;

cURL

如果您的服务器支持cURL (通常如此),则建议使用cURL。使用cURL的一个关键优势是,它依赖于其他编程语言中常用的一个流行的C库。它还为创建请求头和自动解析响应头提供了一种方便的方法,在出现错误时只需一个简单的接口。

代码语言:javascript
复制
$defaults = array( 
    CURLOPT_URL, "http://www.4wtech.com/csp/web/Employee/Login.csp"
    CURLOPT_HEADER=> 0
);

$ch = curl_init(); 
curl_setopt_array($ch, ($options + $defaults)); 
if( ! $result = curl_exec($ch)) { 
    trigger_error(curl_error($ch)); 
} 
curl_close($ch); 
echo $result; 

库

或者,您可以使用之一。不过,我不建议使用库,因为它可能会过度杀伤力。在大多数情况下,最好在幕后使用cURL编写自己的HTTP类。

HTML解析

PHP提供了一种方便的方法将任何超文本标记语言加载到DOMDocument中。

代码语言:javascript
复制
$pagecontent = file_get_contents('http://www.4wtech.com/csp/web/Employee/Login.csp');
$doc = new DOMDocument();
$doc->loadHTML($pagecontent);
echo $doc->saveHTML();

不幸的是,PHP对HTML5的支持是有限的。如果您在尝试分析页面内容时遇到错误,请考虑使用第三方库。为此,我可以推荐。使用该库解析HTML文件与使用DOMDocument解析HTML文件非常相似。

代码语言:javascript
复制
use Masterminds\HTML5;

$pagecontent = file_get_contents('http://www.4wtech.com/csp/web/Employee/Login.csp');
$html5 = new HTML5();
$dom = $html5->loadHTML($html);
echo $html5->saveHTML($dom);

或者,你也可以用例如。我的库。它在幕后使用定制版本的,将HTML5字符串解析为DomDocument,并使用将CSS选择器转换为XPath选择器。它始终使用相同的DomDocument,即使在将一个对象传递给另一个对象时也是如此,以确保良好的性能。

代码语言:javascript
复制
namespace PowerTools;

// Get file content
$pagecontent = file_get_contents( 'http://www.4wtech.com/csp/web/Employee/Login.csp' );

// Define your DOMCrawler based on file string
$H = new DOM_Query( $pagecontent );

// Define your DOMCrawler based on an existing DOM_Query instance
$H = new DOM_Query( $H->select('body') );

// Passing a string (CSS selector)
$s = $H->select( 'div.foo' );

// Passing an element object (DOM Element)
$s = $H->select( $documentBody );

// Passing a DOM Query object
$s = $H->select( $H->select('p + p') );

// Select the body tag
$body = $H->select('body');

// Combine different classes as one selector to get all site blocks
$siteblocks = $body->select('.site-header, .masthead, .site-body, .site-footer');

// Nest your methods just like you would with jQuery
$siteblocks->select('button')->add('span')->addClass('icon icon-printer');

// Use a lambda function to set the text of all site blocks
$siteblocks->text(function( $i, $val) {
    return $i . " - " . $val->attr('class');
});

// Append the following HTML to all site blocks
$siteblocks->append('<div class="site-center"></div>');

// Use a descendant selector to select the site's footer
$sitefooter = $body->select('.site-footer > .site-center');

// Set some attributes for the site's footer
$sitefooter->attr(array('id' => 'aweeesome', 'data-val' => 'see'));

// Use a lambda function to set the attributes of all site blocks
$siteblocks->attr('data-val', function( $i, $val) {
    return $i . " - " . $val->attr('class') . " - photo by Kelly Clark";
});

// Select the parent of the site's footer
$sitefooterparent = $sitefooter->parent();

// Remove the class of all i-tags within the site's footer's parent
$sitefooterparent->select('i')->removeAttr('class');

// Wrap the site's footer within two nex selectors
$sitefooter->wrap('<section><div class="footer-wrapper"></div></section>');
票数 21
EN

Stack Overflow用户

发布于 2009-02-25 06:11:17

您可以使用PHP的cURL扩展从您的PHP页面脚本中向另一个网站发出HTTP请求。请参阅the documentation here.

当然,这样做的缺点是您的站点将响应缓慢,因为在向用户呈现完整页面/输出之前,您将不得不抓取外部web站点。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/584826

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档