循环下载图片,跳过不可用url

#coding utf-8

import cookielib
import urllib2,re,urllib

turl='http://www.test.saofang.cn/service/agent/agentHouseRent.do'
purl='http://www.test.saofang.cn/pub/doAgentLogin.do'   #处理post数据的url


data={'userName':13316750525,'userPwd':123456789}  #post数据,根据实际情况。
post_data=urllib.urlencode(data)  # post数据编码


headers={'Host':'www.test.saofang.cn','Referer':'http://www.test.saofang.cn/pub/agentLogin.do'}


cookieJar=cookielib.CookieJar()   #初始化cookie对象
cookie_support=urllib2.HTTPCookieProcessor(cookieJar)  #处理cookie的程序  用于下载  上传cookie


open=urllib2.build_opener(cookie_support,urllib2.HTTPHandler)


urllib2.install_opener(open)

request=urllib2.Request(purl,post_data,headers)

response=urllib2.urlopen(request) # get cookie

#tn=urllib2.Request(turl)
#tnl=urllib2.urlopen(tn)
tnl=urllib2.urlopen(turl)

text=tnl.read()

req='</strong>/</em><span>.+?(\d+)</b>'   #login success?difference,确认cookie登录成功
lcc=re.search(req,text)
print lcc
tc=re.findall(req,text)
print tc
for list in tc:
    print list




if lcc:
    print 'login success!'
else:
    print 'login fail'
#print lcc.group()

surl='http://www.test.saofang.cn/service/agent/agentHouseRent.do'
type=['Rent','Sold']
tp=['zf','sp','xzl']
http='http://www.test.saofang.cn/service/agent/agentHouse'
zs='.do?houseUsage='
for ty in type:
    for tss in tp:
        # 二手房时参与变化
        if ty=='Sold' and tss=='zf':
            tss='esf'

        https=http+ty+zs+tss
        hss=http+ty+'.do'
        print https
        print hss
    #page=1
        for page in range(1,44):
            data1={'pageIndex':page,'houseState':'PUB','houseUsage':tss}
            print data1
            data=urllib.urlencode(data1)
            headers={'Host':'www.test.saofang.cn','Referer':https}
            reque=urllib2.Request(hss,data,headers)
            #print reque
            t2=urllib2.urlopen(reque)
            rt2=t2.read()
            #print rt2

            reqq="src='(.+?min\.jpg)"
            reqq_list=re.findall(reqq,rt2)
            #print reqq_list
            m=0
            for list in reqq_list:
                s='%d'%m
                p='%d'%page
                #print s, p, list
                print 'e:\\img\\'+ty+tss+p+s+'.jpg'
                sre='http://.+\\.+\\'
                m+=1
                print m
                print list
#检查链接是否可用,不可用时跳过下载图片。
                try:
                    urllib2.urlopen(list).read()
                except:
                    continue
                #保存图片的命名规则
                filename='e:\\img\\'+ty+'_'+tss+'_'+p+'_'+s+'.jpg'
                urllib.urlretrieve(list,filename)

1.下载图片问题1  地址打不开时跳过处理过程

2.Url中有反斜杠,暂时没处理这类情况。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值