鱼C论坛

 找回密码
 立即注册
查看: 3342|回复: 6

爬虫遇到的小问题

[复制链接]
发表于 2015-11-29 23:07:04 | 显示全部楼层 |阅读模式
1鱼币
在爬“优美图”这个网站时,遇到了正则表达式上的一些问题。
网站的图片代码为
  1. <img id="item_d_56716098" class="img ui-draggable" alt="1M" width="245px" height="200px" src="http://fc.topitme.com/c/fb/fc/114846745695efcfbcm.jpg" style="position: relative;">
复制代码

用正则表达式提出图片地址,将表达式写成
  1. p=r'width="245px" height="200px" src="(.*?)"'
复制代码
时可以提取,但是当我用
  1. p=r'width="245px" height="200px" src="(.*?)" style="position: relative;">'
复制代码
这个表达式时不可以。求问是什么原因?谢谢。整个爬虫的完整代码为:
  1. import urllib.request as u
  2. import re
  3. import os

  4. os.mkdir("youmei")
  5. os.chdir("youmei")

  6. def open_url(url):
  7.         req=u.Request(url)
  8.         req.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 UBrowser/5.5.7386.17 Safari/537.36')
  9.         r=u.urlopen(req)
  10.         html=r.read().decode('utf-8')
  11.         return html

  12. def get_img(html):
  13.         p=r'width="245px" height="200px" src="(.*?)" style="position: relative;">'
  14.         imglist=re.findall(p,html,re.S)

  15.         for each in imglist:
  16.                 filename=each.split('/')[-1]
  17.                 u.urlretrieve(each,filename,None)
  18. '
  19. if __name__=='__main__':
  20.         url='http://www.topit.me/pop'
  21.         get_img(open_url(url))
  22.                
复制代码

最佳答案

查看完整内容

1. 楼主你这个url里面的图片的标签不是你这个样的,而是类似这样的,你那样写当然匹配不到 2. 推荐楼主使用re.compile()预编译正则表达式,这样能加快代码执行速度,不必每次执行查找都编译一次 3. 推荐使用环视的语法,这样可以让你的代码可读性增加,具体语法可以看文档,也可以Google一下 4. 稍微注意下正则表达式的贪婪模式和懒惰模式 附我修改的代码,共同学习,一起进步~
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-11-29 23:07:05 | 显示全部楼层
1. 楼主你这个url里面的图片的标签不是你这个样的,而是类似
  1. <img id="item_d_57248120" class="img" alt="1M" width="245px" height="200px" src="http://f3.topitme.com/3/34/65/114942719489a65343m.jpg" />
复制代码
这样的,你那样写当然匹配不到
2. 推荐楼主使用re.compile()预编译正则表达式,这样能加快代码执行速度,不必每次执行查找都编译一次
3. 推荐使用环视的语法,这样可以让你的代码可读性增加,具体语法可以看文档,也可以Google一下
4. 稍微注意下正则表达式的贪婪模式和懒惰模式
附我修改的代码,共同学习,一起进步~
  1. import urllib.request as u
  2. import re
  3. import os
  4. import time

  5. #os.mkdir("youmei")
  6. #os.chdir("youmei")
  7. if not os.path.exists('youmei'):
  8.         os.mkdir('youmei')
  9. os.chdir('youmei')
  10. re_get_img = re.compile(r'(?<=width="245px" height="200px" src=").*?(?=" /></a></div>)') # 预编译正则表达式

  11. def open_url(url):
  12.         req=u.Request(url)
  13.         req.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 UBrowser/5.5.7386.17 Safari/537.36')
  14.         r=u.urlopen(req)
  15.         html=r.read().decode('utf-8')
  16.         return html

  17. def get_img(html):
  18.         #p=r'width="245px" height="200px" src="(.*?)" style="position: relative;">'
  19.         #imglist=re.findall(p,html,re.S)

  20.         #for each in imglist:
  21.         #        filename=each.split('/')[-1]
  22.         #        u.urlretrieve(each,filename,None)

  23.                 img_list = re_get_img.findall(html)
  24.                 for each in range(len(img_list)):
  25.                         print('正在下载第%d张图片...' % (each + 1))
  26.                         try:
  27.                                 img_file = u.urlopen(img_list[each])
  28.                                 f = open(str(each) + '.jpg', 'wb')
  29.                                 #我使用的是原始的open来写数据(小甲鱼老师的坑~),urllib.request.urlretrieve()是很棒的方法,效率甩open几条街,受教了~
  30.                                 f.write(img_file.read())
  31.                                
  32.                         except:
  33.                                 print('第%d张图片下载失败!' % (each + 1))
  34.                                 time.sleep(3)
  35.                         f.close()

  36. if __name__=='__main__':
  37.         url='http://www.topit.me/pop'
  38.         get_img(open_url(url))
  39.                
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2015-11-29 23:08:27 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-11-29 23:52:56 | 显示全部楼层

说来惭愧,正则表达式我也是学艺不精啊~@小甲鱼
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-11-30 11:40:05 | 显示全部楼层
what
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-12-10 09:05:50 | 显示全部楼层
@鱼神解决下,正好我们也学习下(*^__^*) 嘻嘻……
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2015-12-14 23:12:03 | 显示全部楼层
hldh214 发表于 2015-11-29 23:07
1. 楼主你这个url里面的图片的标签不是你这个样的,而是类似这样的,你那样写当然匹配不到
2. 推荐楼主使用r ...

太感谢啦

评分

参与人数 1荣誉 +1 鱼币 +1 收起 理由
hldh214 + 1 + 1 共同进步~~~

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-9-24 02:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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