python用find爬虫提取img下的src属性_Python爬虫抓取图片,网址从文件中读取

利用python抓取网络图片的步骤:

1.根据给定的网址获取网页源代码

2.利用正则表达式把源代码中的图片地址过滤出来

3.根据过滤出来的图片地址下载网络图片

import urllib

import re

import os

#urllib,re,os均为Python模块

def gethtml(outline):

page = urllib.urlopen(outline)              #抓取网页内容获得图片链接

html = page.read()

return html

def getimg(html):                                #下载图片保存在同目录下的pictures文件夹下

reg=r'src="(.+?\.jpg)" pic_ext'

imgre=re.compile(reg)

imglist=imgre.findall(html)

if not imglist:

print "not found"

else:

filepath=os.getcwd() +'\pictures'

print filepath

if os.path.exists(filepath) is False:

os.mkdir(filepath)

global x

for imgurl in imglist:

temp = filepath + '\%s.jpg' % x

print imgurl

urllib.urlretrieve(imgurl,temp)

x=x+1

x = 0

fp =file("img_path.txt")                          #所有网址都放在这个文件里

while True:

outline = fp.readline().strip('\n')

if len(outline)==0:

break

print outline

html=gethtml(outline)

getimg(html)

fp.close()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值