search

完成了大约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);       }

//问题出在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
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
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

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 Article

Hot Tools

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),

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools