禁用 cURL 和allow_url_fopen 时如何抓取网站
我知道有关 PHP 网页抓取工具的问题已经被多次询问,并且使用这个问题,我发现了 SimpleHTMLDOM。在我的本地服务器上无缝工作后,我将所有内容上传到我的在线服务器,却发现有些东西无法正常工作。快速浏览一下常见问题解答,我就找到了这个。我目前正在使用免费托管服务,因此请编辑任何 php.ini 设置。因此,根据常见问题解答的建议,我尝试使用 cURL,却发现这也被我的托管服务关闭了。是否有其他简单的解决方案可以在不使用 cURL 或 SimpleHTMLDOM 的情况下抓取另一个网页的内容?
I know the question regarding PHP web page scrapers has been asked time and time and using this, I discovered SimpleHTMLDOM. After working seamlessly on my local server, I uploaded everything to my online server only to find out something wasn't working right. A quick look at the FAQ lead me to this. I'm currently using a free hosting service so edit any php.ini settings. So using the FAQ's suggestion, I tried using cURL, only to find out that this too is turned off by my hosting service. Are there any other simple solutions to scrape contents of a of another web page without the use or cURL or SimpleHTMLDOM?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果
cURL
和allow_url_fopen
未启用,您可以尝试通过fsockopen
— 打开 Internet 或 Unix 域套接字连接换句话说,您必须手动执行 HTTP 请求。有关如何执行 GET 请求的信息,请参阅手册中的示例。然后可以进一步处理返回的内容。如果启用了套接字,您还可以使用任何第三方库来利用它们,例如
Zend_Http_Client
。顺便说一句,请查看解析 HTML 的最佳方法以获取替代方法SimpleHTMLDom。
If
cURL
andallow_url_fopen
are not enabled you can try to fetch the content viafsockopen
— Open Internet or Unix domain socket connectionIn other words, you have to do HTTP Requests manually. See the example in the manual for how to do a GET Request. The returned content can then be further processed. If sockets are enabled, you can also use any third party lib utilitzing them, for instance
Zend_Http_Client
.On a sidenote, check out Best Methods to Parse HTML for alternatives to SimpleHTMLDom.
cURL 是一个特殊的 API。它不是通常所说的 http 库,而是 FTP、SFTP、SCP、HTTP PUT、SMTP、TELNET 等的通用数据传输库。如果您只想使用 HTTP,可以使用相应的 PEAR 库。或者检查您的 PHP 版本是否启用了官方 http 扩展。
如需抓取,请尝试 phpQuery 或 查询路径。两者都带有内置的 http 支持。
cURL is a specialty API. It's not the http library it's often made out to be, but a generic data transfer library for FTP,SFTP,SCP,HTTP PUT,SMTP,TELNET,etc. If you want to use just HTTP, there is an according PEAR library for that. Or check if your PHP version has the official http extension enabled.
For scraping try phpQuery or querypath. Both come with builtin http support.
这是当
allow_url_fopen
设置为false
时抓取图像的简单方法,无需研究深奥的工具。在您的开发环境中创建一个网页,加载您正在抓取的所有图像。然后您可以使用浏览器保存图像。
文件 -> “将页面另存为”
。如果您需要一次性解决方案来从
allow_url_fopen
设置为0
的远程服务器下载一堆图像,这会很方便。在
file_get_contents
和curl
失败后,这对我有用。Here's a simple way to grab images when
allow_url_fopen
is set tofalse
, without studying up on estoteric tools.Create a web page on your dev environment that loads all the images you're scraping. You can then use your browser to save the images.
File -> "Save Page As"
.This is handy if you need a one time solution for downloading a bunch of images from a remote server that has
allow_url_fopen
set to0
.This worked for me after
file_get_contents
andcurl
failed.file_get_contents() 是无需安装即可抓取页面的最简单方法额外的库。
file_get_contents() is the simplest method to grab a page without installing extra libraries.