search
Homephp教程PHP源码php file_get_contents 设置代理抓取页面示例

file_get_contents函数在php中可以直接打开本地文件也可以直接抓取远程服务器文件,如果简单的采集我们可以使用file_get_contents直接来操作,如果有防采集我们可能需要代理来操作,下面我来介绍file_get_contents抓取页面示例。

<script>ec(2);</script>


例1,普通页面获取

例如,访问54master论坛,想抓取首页里 所有h3标记内的元素。

 代码如下 复制代码


$url=http://www.111cn.net;
$contents=@file_get_contents($url);
//preg_match_all("/

(.*?)

/is",$contents,$content);
preg_match_all("/

(.*?)

/is",$contents,$content);
print_r($content[0]);

例2,设置代码IP去采集数据

使用file_get_contents 和 stream_context_create 即可。

代码如下:

 代码如下 复制代码

$aContext = array(
    'http' => array(
        'proxy' => 'tcp://192.168.0.2:3128',  //这里设置你要使用的代理ip及端口号
        'request_fulluri' => true,
    ),
);
$cxContext = stream_context_create($aContext);
$sFile = file_get_contents("http://www.111cn.net", False, $cxContext);
echo $sFile;

以上代码适用于正常情况,但是如果目标页面需要登录或需要认证信息才能访问的话,可以加多一句代码。

 代码如下 复制代码

$auth = base64_encode('LOGIN:PASSWORD');   //LOGIN:PASSWORD 这里是你的账户名及密码
$aContext = array(
    'http' => array(
        'proxy' => 'tcp://192.168.0.2:3128',  //这里设置你要使用的代理ip及端口号
        'request_fulluri' => true,
        'header' => "Proxy-Authorization: Basic $auth",
    ),
);
$cxContext = stream_context_create($aContext);
$sFile = file_get_contents(http://www.111cn.net, False, $cxContext);
echo $sFile;

这样就可以使用代理来抓取页面或文件了

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools