search
HomeBackend DevelopmentPHP Tutorialphp同步大量采集,超难问题,新手勿进,谢谢

1、程序类似myip.cn/wanben.net ,他的站可以在3秒内采集出wanben.net的所有信息。

2、我通过php也可以完成采信wanben.net的全部信息,但速度太慢,如
   采集网站title
   采集alexa信息
   采集域名信息
   采集服务器信息,
 
   我通过php写的程序,要顺序执行所有代码。所以时间很长,全部采集完成要15秒左右

而myip.cn采集上面同样多的信息,用时3秒左右。


高手请回答,是用php+ajax还是用的php同步批量采集做的,请给出原理

我的站wanben.net采集全是我自己 写的,现在就要是实现大批量快速采集并返回值。




回复讨论(解决方案)

1、程序类似myip.cn/wanben.net ,他的站可以在3秒内采集出wanben.net的所有信息。

2、我通过php也可以完成采信wanben.net的全部信息,但速度太慢,如
  采集网站title
  采集alexa信息
  采集域名信息
  采集服务器信息,
 
  我通过php写的程序,要顺序执行所有代码。所以时间很长,全部采集完成要15秒左右

而myip……

数据采集建议使用CURL来完成 


PHP的cURL库功能简介:抓取网页,POST数据及其他


本文介绍了PHP的cURL库的几个使用方法。cURL是一个功能强大的PHP库,可以用于获取网页内容,获取网页内容以及取一个XML文件并把其导入数据库等等。
使用PHP的cURL库可以简单和有效地去抓网页。你只需要运行一个脚本,然后分析一下你所抓取的网页,然后就可以以程序的方式得到你想要的数据了。无论是你想从从一个链接上取部分数据,或是取一个XML文件并把其导入数据库,那怕就是简单的获取网页内容,cURL 是一个功能强大的PHP库。本文主要讲述如果使用这个PHP库。

启用 cURL 设置

首先,我们得先要确定我们的PHP是否开启了这个库,你可以通过使用php_info()函数来得到这一信息。

??phpphpinfo();??


 

如果你可以在网页上看到下面的输出,那么表示cURL库已被开启。

如果你看到的话,那么你需要设置你的PHP并开启这个库。如果你是在Windows平台下,那么非常简单,你需要改一改你的php.ini文件的设置,找到php_curl.dll,并取消前面的分号注释就行了。如下所示:

//取消下在的注释extension=php_curl.dll

 

如果你是在Linux下面,那么,你需要重新编译你的PHP了,编辑时,你需要打开编译参数??在configure命令上加上“?with-curl” 参数。

一个小示例

如果一切就绪,下面是一个小例程:

??php
// 初始化一个 cURL 对象
$curl = curl_init(); 

// 设置你需要抓取的URL
curl_setopt($curl, CURLOPT_URL, 'http://cocre.com');

// 设置header
curl_setopt($curl, CURLOPT_HEADER, 1);

// 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

// 运行cURL,请求网页
$data = curl_exec($curl);

// 关闭URL请求
curl_close($curl);

// 显示获得的数据
var_dump($data);
 

如何POST数据

上面是抓取网页的代码,下面则是向某个网页POST数据。假设我们有一个处理表单的网址http://www.example.com/sendSMS.php,其可以接受两个表单域,一个是电话号码,一个是短信内容。

??php$phoneNumber = '13912345678';$message = 'This message was generated by curl and php';$curlPost = 'pNUMBER='  . urlencode($phoneNumber) . '&MESSAGE=' . urlencode($message) . '&SUBMIT=Send';$ch = curl_init();curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/sendSMS.php');curl_setopt($ch, CURLOPT_HEADER, 1);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);$data = curl_exec();curl_close($ch);??

 

从上面的程序我们可以看到,使用CURLOPT_POST设置HTTP协议的POST方法,而不是GET方法,然后以CURLOPT_POSTFIELDS设置POST的数据。

关于代理服务器

下面是一个如何使用代理服务器的示例。请注意其中高亮的代码,代码很简单,我就不用多说了。

??php $ch = curl_init();curl_setopt($ch, CURLOPT_URL, 'http://www.example.com');curl_setopt($ch, CURLOPT_HEADER, 1);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);curl_setopt($ch, CURLOPT_PROXY, 'fakeproxy.com:1080');curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'user:password');$data = curl_exec();curl_close($ch);??
       

关于SSL和Cookie

关于SSL也就是HTTPS协议,你只需要把CURLOPT_URL连接中的http://变成https://就可以了。当然,还有一个参数叫CURLOPT_SSL_VERIFYHOST可以设置为验证站点。

关于Cookie,你需要了解下面三个参数:

CURLOPT_COOKIE,在当面的会话中设置一个cookie 

CURLOPT_COOKIEJAR,当会话结束的时候保存一个Cookie 

CURLOPT_COOKIEFILE,Cookie的文件。 

HTTP服务器认证

最后,我们来看一看HTTP服务器认证的情况。

??php 
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt(CURLOPT_USERPWD, '[username]:[password]')

$data = curl_exec();
curl_close($ch);
??
 

关于其它更多的内容,请参看相关的cURL手册。

官方CURL中文文档:

curl_setopt

问下楼主,如果网络环境不好,或者网站数据量大,或者php程序结构执行比较费时间,php执行超时怎么办?

引用楼主 mywaster 的回复:
1、程序类似myip.cn/wanben.net ,他的站可以在3秒内采集出wanben.net的所有信息。

2、我通过php也可以完成采信wanben.net的全部信息,但速度太慢,如
采集网站title
采集alexa信息
采集域名信息
采集服务器信息,

我通过php写的程序,要顺序执行所有代码。所以时间很长,全部采集完成要15秒左右……

我就是忘记说了,我就是用php curl写的采集,请根据我的问题回答


采集网站title
采集alexa信息
采集域名信息
采集服务器信息

想同时进行怎么办??????????

有能力自己写扩展呗,多线程,云计算

引用 1 楼 skyaspnet 的回复:
引用楼主 mywaster 的回复:
1、程序类似myip.cn/wanben.net ,他的站可以在3秒内采集出wanben.net的所有信息。

2、我通过php也可以完成采信wanben.net的全部信息,但速度太慢,如
采集网站title
采集alexa信息
采集域名信息
采集服务器信息,

我通过php写的程序,要顺序执行……



采集网站title
这个很简单,在获取到数据后直接正则即可取出。

采集alexa信息
这个需要向alexa发送查询即可获取到数据,建议和获取TITLE的操作分开。

采集域名信息
这一步也同样需要发送查询命令,建议也分开做

采集服务器信息
服务器信息只能获取到很少的一部分,例如一些头信息,大部分是获取不到的


想要加快速度的话,建议将一系列操作通过一定的方法关联起来,然后再分步执行,这样速度会有比较明显的提高

我测试了一下
首次在 myip.cn上检索wanben.com耗时16秒(当然,我的电脑显示有点慢)
再次在 myip.cn上检索仅仅两秒

所以可以肯定myip.cn用了缓存,你后来看到的检索结果都是从缓存中调出的,所以很快。

我自己解决了

谢谢大伙的思路,看看我空间的办法
http://hi.baidu.com/dalianufo/blog/item/c70ef1d9a1a92a3f10df9b0a.html

http://www.chaiba.com 也很快的,原理类似

http://www.chayiba.com 这个

如果url下面还有子链接呢?例如http://news.ifeng.com/mil/,下面还有子栏目,子链接,该怎么办呢?

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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

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

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

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.

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.