专注于自动化测试,性能测试.......

使用python测试网页中超链接的连通性(原创)

上一篇 / 下一篇  2009-09-21 22:24:27 / 个人分类:Python

51Testing软件测试网R*d MW4r yHV R

  在web测试,对网页中的超链接进行测试是最基本的工作,最简便的方法当然是使用像xenu之类的工具。但它具体是怎么实现的呢?我想也无外乎是通过http协议,根据超链接地址,向服务端发送请求,然后根据返回的信息进行判断连接的状态。下面是根据这种思路,用python编写的检测网页链接连通性的程序。51Testing软件测试网XT]$j d+{u7a

51Testing软件测试网 Z{!f;FU%z"G} ka

首先,建立一个示例网页,其中link1,lin3是不连通的,link2,link4是有效链接

7` pJ.Qu3[0

H rM Y?9SOs0s0<head>Test</hest>
j qe{xF0<body>51Testing软件测试网 X#{,W!U1m G8r
 <a href="http://ggdfgdfg.com/erwerwe.html">link1</a>
\,XNF9v?T U"l4Za0 <a href="/sample/lik.html">link2</a>51Testing软件测试网xR6F5ga.H4m-t
 <a href="/sample/lik2.html">link3</a>51Testing软件测试网/K:d [&\,SG~{
 <a href="http://google.com">link4</a>
%l1r ]9Ii@0</body>51Testing软件测试网!J-K&p7` \oV

51Testing软件测试网N4_;B`1D,`1E

使用python进行链接检测,要使用到4个重要模块,过程就是通过urllib抓取目标网页的html代码,然后通过sgmllib模块解析html,获取超链接的列表。然后使用urlparse解析超链接的url,供httplib使用。然后由httplib模块进行最后的请求及验证回复的过程。

K(l k(_;oj9X2K8}0

VZH'BT8p w,q,Kz@0sgmllib :用于HTML解析,解析出网页中包含的超链接

(lL"b"g gj051Testing软件测试网V-H:kt8D!V0w!K

httplib:用于Http协议的操作

4h2fhY#?]051Testing软件测试网#|s$gMG0[

urllib:用于获取网页的html代码51Testing软件测试网)_%hK|$kA

51Testing软件测试网JyeA){7rb(dE

urlparse:解析url地址,把url地址解析成几个部分。

$q/M+{7`0[dT0

cl T:K Rh0具体实现代码如下:51Testing软件测试网 xL/Tn@J*wi5a,ER

Uq4q1xozI y0#-×-coding:gb2312-*-51Testing软件测试网s/|ELI%BJ*R)P
import httplib,urllib,urlparse51Testing软件测试网Q4r'nU"?B
from sgmllib import SGMLParser51Testing软件测试网]reF pBgA;s
#解析指定的网页的html,得到该页面的超链接列表51Testing软件测试网(PJ}e*W;R
class URLLister(SGMLParser):51Testing软件测试网4nZ Q/gp;Jy
    def reset(self):
$hlH7Flh:s!H0        SGMLParser.reset(self)51Testing软件测试网@(xN7P2O0r
        self.urls = []

(a ?c$|e/U/mJ#p?051Testing软件测试网;~NA*X,@/{ x6M

    def start_a(self, attrs):
g6v.BXv!Z j0        href = [v for k, v in attrs if k=='href']51Testing软件测试网Gmb'pQ2ysp
        if href:
~!v _\!Z6L&a0            self.urls.extend(href)
P0h0o|%] ]!o1n}$E0#遍历超链接列表,并逐个的发送请求,判断接收后的代码,200为正常,其他为不正常51Testing软件测试网,?uFm-ik%kd7? dL-u
def fetch(host):51Testing软件测试网.X9c3a$jYiC
        usock = urllib.urlopen(host)
9b.kf ^!y%[-b}Ia"V0        parser = URLLister()51Testing软件测试网vO8o4|"H6J
        parser.feed(usock.read())
7B4bT+dv'kl0        uhost = urlparse.urlparse(host)
d"x a&mct5K0        for url in parser.urls:
~H8} s&CO0            up = urlparse.urlparse(url)
2ow:cx p0            #因为超链接有两种方式:一种是直接的http://...... 一种是相对路径,/.../sample.html
3VRrH,WVeh.}0          #所以要分别处理51Testing软件测试网jdQ L*Utu
            if up.netloc =="":51Testing软件测试网,NDt/}y3FW:B4B
                http = httplib.HTTP(uhost.netloc)51Testing软件测试网(H7` LbAu2x!OP(wH
                http.putrequest("GET", "/"+up.path+"?"+up.params+up.query+up.fragment)51Testing软件测试网-P d9]3V0rb
                http.putheader("Accept", "*/*")51Testing软件测试网3C;V6M6FYU;Q
                http.endheaders()51Testing软件测试网,V(v)LH"O8XqR q`k
            else:
~Q2p,M:fg(t G0_ L1V0                http = httplib.HTTP(up.netloc)
%]lg2Y1Q8\!l _3H0                http.putrequest("GET", up.path+"?"+up.params+up.query+up.fragment)
K:PM&N1IHv.T^0                http.putheader("Accept", "*/*")51Testing软件测试网5Z#i2[J;p+]vl [
                http.endheaders()
$Uxsd7m(sVT0            errcode, errmsg, headers = http.getreply()51Testing软件测试网eh]J L
            if errcode == 200:
1ii}5C#~O;J)mU0                print url," : ok"
3A!Bq:l%}Ezv0            else:
:r(T5_WW"m{+I0                print url," : ",errcode

{+t!FKm:yV}0

g;Q`+b\yq0#测试51Testing软件测试网j5? U R'C+BJ/_ Gu
fetch("http://localhost/Sample/sample.html")51Testing软件测试网)k;nYPN6^h

K0^v |"mK:Xs0代码运行的结果:51Testing软件测试网+V(x7Q{{'S z[3x

51Testing软件测试网lG1mbu3q3}

http://ggdfgdfg.com/erwerwe.html  :  40451Testing软件测试网%\m]'FVl.r!_)X`
/sample/lik.html  : ok
G2IL M!k1U1Kl0/sample/lik2.html  :  40451Testing软件测试网%k HB9Gk9V A_m
http://google.com  : ok51Testing软件测试网ja*e+w;tsb3V.Yp

M#Z;bk'd T"i0 51Testing软件测试网:RCsb7z6Y)M2h

GQ%Y%s O0版权声明:本文出自wxf_xsfy的51Testing软件测试博客:http://www.51testing.com/?61753

原创作品,转载时请务必以超链接形式标明文章原始出处、作者信息和本声明,否则将追究法律责任。
8v!Fs]2U(U0

U~8NlEl _(|0

TAG: Python

学走路的小蜗牛 引用 删除 大地的女儿   /   2016-03-16 18:07:37
报这个错:
Traceback (most recent call last):
  File "/Users/xiaoju/PycharmProjects/untitled/Hyperlink.py", line 3, in <module>
    import httplib
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 1151, in <module>
    import ssl
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 58, in <module>
    import textwrap
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/textwrap.py", line 40, in <module>
    class TextWrapper:
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/textwrap.py", line 82, in TextWrapper
    whitespace_trans = string.maketrans(_whitespace, ' ' * len(_whitespace))
AttributeError: 'module' object has no attribute 'maketrans'
yanlf996的个人空间 引用 删除 yanlf996   /   2014-04-03 16:51:37
原帖由yanlf996于2014-04-03 16:50:24发表

点错了
yanlf996的个人空间 引用 删除 yanlf996   /   2014-04-03 16:50:24
-5
trilcc的个人空间 引用 删除 trilcc   /   2013-09-12 16:46:27
5
 

评分:0

我来说两句

wxf_xsfy

wxf_xsfy

自动化测试的拥簇者,善于自动化测试的框架和工具开发,TIB工作室核心成员

日历

« 2024-09-17  
1234567
891011121314
15161718192021
22232425262728
2930     

数据统计

  • 访问量: 384346
  • 日志数: 79
  • 图片数: 1
  • 文件数: 1
  • 书签数: 3
  • 建立时间: 2007-09-19
  • 更新时间: 2018-01-30

RSS订阅

Open Toolbar