Table of Contents
回复讨论(解决方案)

php采集程序

Jun 23, 2016 pm 01:43 PM

完成了大约10分钟的采集后,出现fwrite(......)请求,出现警告,随后出现丢包,有些网页就出现抓取失败的问题。。

前面10分钟都没问题,10分钟之后,fwrite(....)发送请求出现警告的频率慢慢变大

难道是被服务器拒绝了请求?

错误提示信息:

Notice: fwrite(): in E:\CodeEdit\php\http\001.php on line 200
对链接http://www.mmkao.com/Beautyleg/201410/6565_6.html发起请求
即将采集下一组
对链接http://www.mmkao.com/Beautyleg/201410/6559.html发起请求
对链接http://www.mmkao.com/Beautyleg/201410/6559_2.html发起请求
对链接http://www.mmkao.com/Beautyleg/201410/6559_3.html发起请求
对链接http://www.mmkao.com/Beautyleg/201410/6559_4.html发起请求
对链接http://www.mmkao.com/Beautyleg/201410/6559_5.html发起请求
对链接http://www.mmkao.com/Beautyleg/201410/6559_6.html发起请求
对链接http://www.mmkao.com/Beautyleg/201410/6559_7.html发起请求
对链接http://www.mmkao.com/Beautyleg/201410/6559_8.html发起请求
对链接http://www.mmkao.com/Beautyleg/201410/6559_9.html发起请求
对链接http://www.mmkao.com/Beautyleg/201410/6559_10.html发起请求
即将采集下一组
对链接http://www.mmkao.com/Beautyleg/201410/6549.html发起请求
对链接http://www.mmkao.com/Beautyleg/201410/6549_2.html发起请求

Notice: fwrite(): in E:\CodeEdit\php\http\001.php on line 200
对链接http://www.mmkao.com/Beautyleg/201410/6549_3.html发起请求
即将采集下一组

Notice: fwrite(): in E:\CodeEdit\php\http\001.php on line 200
对链接http://www.mmkao.com/Beautyleg/201410/6537.html发起请求

Notice: fwrite(): in E:\CodeEdit\php\http\001.php on line 200

有点疑惑,代码的实现原理还是很简单的,只是网络请求调试想对困难一些,有这方面经验的,可以分享一下


代码有点多?居然提示不能超过10000个字符。 
代码链接在这里:      http://blog.csdn.net/free_program_1314/article/details/41798199


回复讨论(解决方案)

你在 100 行处 $this->conn = fsockopen($this->ip,$this->port,$errno,$errstr,$this->timeout);
后只在 101 行判断了是否连接成功,虽然不成功时会返回 false
但在 62 行调用时 $this->connect(); 并没有检查是否返回了 false
即无论连接是否成功,程序都会继续执行。于是很自然的把隐患扩散了出去
你至少得在 200 行的 fwrite($this->conn,.... 前,判断一下 $this->conn 是否是资源

而若 $this->connect() 失败,就该转到策略处理程序,还按正常流程进行是没有意义的

你在 100 行处 $this->conn = fsockopen($this->ip,$this->port,$errno,$errstr,$this->timeout);
后只在 101 行判断了是否连接成功,虽然不成功时会返回 false
但在 62 行调用时 $this->connect(); 并没有检查是否返回了 false
即无论连接是否成功,程序都会继续执行。于是很自然的把隐患扩散了出去
你至少得在 200 行的 fwrite($this->conn,.... 前,判断一下 $this->conn 是否是资源

而若 $this->connect() 失败,就该转到策略处理程序,还按正常流程进行是没有意义的



if($this->conn)       {           if($len != fwrite($this->conn, $this->cmd_line.$this->header.$this->post_content,$len))            {                $this->status = "发送请求failed";                die($this->status);            }       }       else       {           die($this->status);       }
Copy after login

//问题出在fwrite(....)发送请求失败这里。。。
Notice: fwrite(): in E:\CodeEdit\php\http\001.php on line 213
发送请求failed

你没有判断那个 $this->conn 是否有效

你没有判断那个 $this->conn 是否有效



稍加改进了一些,链接这些都没问题,现在的错误提示是:

对链接http://www.mmkao.com/Beautyleg/201412/7066_9.html发起请求
没有下一页,原因是:匹配image分页组失败

但是getRollLink这个匹配下一页的函数并没有什么问题,我反复测试过,即便从本地保存的网页里读取也没问题,而且在出现这个问题之前,也采集了很多网页,也没出现问题。 

对链接http://www.mmkao.com/Beautyleg/201412/7066_5.html发起请求
对链接http://www.mmkao.com/Beautyleg/201412/7066_6.html发起请求
对链接http://www.mmkao.com/Beautyleg/201412/7066_7.html发起请求
对链接http://www.mmkao.com/Beautyleg/201412/7066_8.html发起请求
对链接http://www.mmkao.com/Beautyleg/201412/7066_9.html发起请求
没有下一页,原因是:匹配image分页组失败

改进后的发在这里了:  http://blog.csdn.net/free_program_1314/article/details/41805697

举例说明这个存在的问题,比如采集完第九个网页之后,采集第十页的时候出现匹配失败,而这个采集第十页的链接是第九页的网页信息提供的。  打开采集到的第九个文件,读取并匹配下一页的链接是没有问题的。出现这样的情况多次,每次我都尝试从文件读取并匹配下一页的链接都是没问题的。


也就是说匹配下一页的链接,如果有问题,刚开始采集就应该出现这个提示。。。现在的问题是,这个问题不定期出现

不定期出现是正常的,就算是用浏览器访问,偶尔出现页面打不开(刷新一下就好了)不也是正常的吗?
你只要随时检查连接的有效性,并在失效时做出处理就可以了。比如等几秒再次连接

不定期出现是正常的,就算是用浏览器访问,偶尔出现页面打不开(刷新一下就好了)不也是正常的吗?
你只要随时检查连接的有效性,并在失效时做出处理就可以了。比如等几秒再次连接



偶明白了 

不定期出现是正常的,就算是用浏览器访问,偶尔出现页面打不开(刷新一下就好了)不也是正常的吗?
你只要随时检查连接的有效性,并在失效时做出处理就可以了。比如等几秒再次连接



这个问题通过检测到匹配失败时,从保存的上一页网页中,提取下一页链接,暂时解决了:

对链接http://www.mmkao.com/Beautyleg/201411/6906_13.html发起请求
没有下一页,原因是:匹配image分页组失败,尝试从文件中读取匹配模式
对链接http://www.mmkao.com/Beautyleg/201411/6906_14.html发起请求
即将采集下一组
对链接http://www.mmkao.com/Beautyleg/201411/6892.html发起请求
对链接http://www.mmkao.com/Beautyleg/201411/6892_2.html发起请求
对链接http://www.mmkao.com/Beautyleg/201411/6892_3.html发起请求


采集了10分钟之后,还是出现请求失败的问题,,,fwrite(),又是这个老问题,应该可以通过重置连接解决

看起来很NB的样子

Statement of this Website
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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

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.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 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.

How do you prevent SQL Injection in PHP? (Prepared statements, PDO) How do you prevent SQL Injection in PHP? (Prepared statements, PDO) Apr 15, 2025 am 12:15 AM

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles