欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

从某网站抓取图片并自动下载到文件夹内

程序员文章站 2022-03-28 23:02:28
...
。。。。因为某网站看图比较坑爹,要一页一页的翻页。。。。所以。。。。就写了这么个东西
(我是产品不是程序员)运行速度简直无法忍受,而且经常会有错误发生,所以希望大家帮忙改进(PHP)。。。
当然也欢迎看到PYTHON,GOLANG的版本~~^_^
对了,程序基于CodeIgniter
。。。。链接谨慎点击
  1. $this->load->helper('date');
  2. $this->load->helper('phpQuery');
  3. //我是把phpQuery单文件放到helper里了
复制代码
  1. //- -只是为了快速出产品,所以直接在VIEW里写的代码,请无视变量名。。。
  2. $imageslist = phpQuery::newDocumentFile('http://g.e-hentai.org/g/xxxxx/xxxxxxx/');//首页
  3. $pn = 1;//页数
  4. $ps = $imageslist->find('.ptt td a');//从首页抓页面导航
  5. //从页面导航开始获取页面内容
  6. foreach($ps as $p)
  7. {
  8. echo '第'.$pn++.'页:';
  9. $imagesnow = phpQuery::newDocumentFile(pq($p)->attr("href"));//单页内容
  10. $images = $imagesnow->find('#gdt a');//抓取图片页列表
  11. foreach($images as $image)
  12. {
  13. echo '';
  14. $imagebigs = phpQuery::newDocumentFile(pq($image,$imagesnow)->attr("href"));//获取单图片页地址
  15. echo '从某网站抓取图片并自动下载到文件夹内';//输出图片
  16. ob_start();
  17. readfile($imagebigs->find('#i3 img')->attr('src'));
  18. $img = ob_get_contents();
  19. ob_end_clean();
  20. $filename='img/'.now().'.jpg';
  21. $f=fopen($filename,'a');
  22. fwrite($f,$img);
  23. fclose($f);
  24. }
  25. }
  26. ?>
复制代码