鱼C论坛

 找回密码
 立即注册
查看: 5053|回复: 2

[已解决]Python 2.7爬虫爬取图片,为什么不能下载到本地?

[复制链接]
发表于 2017-3-7 17:25:21 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

  1. #!/usr/bin/python
  2. # -*- coding: UTF-8 -*-
  3. import re
  4. import urllib

  5. def getHtml(url):
  6.     response=urllib.urlopen(url)
  7.     html=response.read()
  8.     return html

  9. def getImg(html):
  10.     reg=r'src="(.+?\.png)" alt'
  11. #   re.compile() 可以把正则表达式编译成一个正则表达式对象
  12.     imgre=re.compile(reg)
  13. #   re.findall() 方法读取html 中包含 imgre(正则表达式)的数据
  14.     imglist=re.findall(imgre,html)
  15.     i=0
  16.     for img in imglist:
  17.         urllib.urlretrieve(img,'%s.png'%i)
  18.         i+=1

  19. html=getHtml("http://gl.sanguosha.com/")
  20. print getImg(html)
复制代码
最佳答案
2017-3-7 20:30:02
木有报错内容,差评!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-3-7 20:30:02 | 显示全部楼层    本楼为最佳答案   
木有报错内容,差评!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-3-8 12:38:13 | 显示全部楼层
~风介~ 发表于 2017-3-7 20:30
木有报错内容,差评!

可是用Pycharm ,运行会报错。就没法下载到本地
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-17 06:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表