分享教程:搜索网站里的图片,检查是否是无效图片。

目前组里面测试新同学很多时候不知道如何将自己学的编码知识和测试场景实际结合起来,这里拿1个实际例子,分析下过程。

  • 拿到1个需要编码的测试需求,怎么做

测试需求:公司网站里面商品图片很多是无效链接,检查搜索出来,进行进一步处理。

一,思考:首先想下怎么做?  

  1. 思路:搜索出所有图片链接----->访问这些链接,查看response-------> http code 非 200 的为无效链接
  2. 用什么语言:目前流行的语言java,Python,都可以做,最后选择Python,处理类似爬虫工作,python比较简洁方便,而且库很多。
  3. 不了解Python库:查看经典库 https://github.com/jobbole/awesome-python-cn,找到处理http请求的; 或者直接百度/google 搜索 Python url

 

二,怎么找出网站里的所有图片链接

  1. 打开公司网站,右键---查看网站源代码,会出来一大堆html code;我们就是要把里面的图片链接给找出来
  2. code:将网站的源代码输出到buffer,再进行出来处理
    import urllib2 #导入urllib2模块
    req = urllib2.urlopen('https://www.zcy.gov.cn/')
    buf = req.read()
    print buf 
       
  3. 查看里面的图片链接:可以观察到图片链接的主要形式:<img alt="" style="width: 100%" src="http://zcy-item.img-cn-hangzhou.aliyuncs.com//2017062111022565214060.png"> 
  4. 通过正则表达式来搜索:""<img\s.*?\s?src\s*=\s*['|"]?([^\s'"]+).*?>""
  5. code:
    import re# 正则表达式模块
    imagelist = re.findall(r"""<img\s.*?\s?src\s*=\s*['|"]?([^\s'"]+).*?>""",buf)
    print imagelist
    
    输出:['http://zcy-item.img-cn-hangzhou.aliyuncs.com//2017060816344209047406.png', 'http://zcy-item.img-cn-hangzhou.aliyuncs.com//2017111717221734892422.gif'。。。。。]
    

     

  6. 从image list里面对每个链接就行http请求,记录非200的链接
  7. import sys
    import requests
    for imageurl in imagelist:
        print imageurl
        req1 = requests.get(imageurl)
        print req1.status_code
        if req1.status_code == 200:
            with open("d:/test/200.txt",'a') as f:
                f.write(imageurl+"\n")
        else:
            with open("d:/test/not200.txt",'a') as f:
                f.write(imageurl+"\n")      
    

      

  8. 未完待续  

 

posted @ 2017-12-11 09:36  臭臭爸爸  阅读(669)  评论(0编辑  收藏  举报