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


    目前组里面测试新同学很多时候不知道如何将自己学的编码知识和测试场景实际结合起来,这里拿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=" 100%" src="http://zcy-item.img-cn-hangzhou.aliyuncs.com//2017062111022565214060.png"> 
    4. 通过正则表达式来搜索:""<imgs.*?s?srcs*=s*['|"]?([^s'"]+).*?>""
    5. code:
      import re# 正则表达式模块
      imagelist = re.findall(r"""<imgs.*?s?srcs*=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+"
      ")
          else:
              with open("d:/test/not200.txt",'a') as f:
                  f.write(imageurl+"
      ")      
      

        

    8. 未完待续  

  • 相关阅读:
    魔法变量*args 和 **kwargs
    windows下怎么安装protobuf for python
    正向代理与反向代理
    Python 中 "is" 与 "==" 操作有什么区别?
    用 Anaconda 完美解决 Python2 和 python3 共存问题
    Python爬虫实例
    安装包制作工具 SetupFactory使用2 API清单
    软件测试流程(Test Flow)
    从一个实例详解敏捷测试的最佳实践
    网络常用基础知识大全
  • 原文地址:https://www.cnblogs.com/appstest/p/7986123.html
Copyright © 2020-2023  润新知