我的测试人生........

【转】使用python测试网页中超链接的连通性

上一篇 / 下一篇  2013-09-17 23:16:20 / 个人分类:测试技术

http://www.51testing.com/html/53/61753-155629.html

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

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

I.S#TM D9|0

*NhUy5FM?_0<head>Test</hest>
u G K4z ~U;h NHE}0<body>51Testing软件测试网&?q$d/Hk"v2@
<a href="http://ggdfgdfg.com/erwerwe.html">link1</a>
$_V"j8WT Y6O j"L0<a href="/sample/lik.html">link2</a>
b!R3Zi!{@$g0n e0<a href="/sample/lik2.html">link3</a>
^^ ]s#KL.e0<a href="http://google.com">link4</a>
$@/}#q3e D D-D0</body>51Testing软件测试网-p f8U:D wXi~

51Testing软件测试网C4t\p|B ^@ L

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

51Testing软件测试网8xd-i&I7z:w#X2ZI!A"n

sgmllib :用于HTML解析,解析出网页中包含的超链接

.b/{4w-^-~$q0

3FHV8M~T\7G0httplib:用于Http协议的操作

P/x*Rk%d,x!FJ7g7V0

3[c4OLQo0urllib:用于获取网页的html代码51Testing软件测试网GfT| J&o+~!_

$yT6M+qYpQ0urlparse:解析url地址,把url地址解析成几个部分。51Testing软件测试网ie,US cFH

0T Q/Ls;SAL0具体实现代码如下:

+r"FK;?B4y^i[051Testing软件测试网%|x{sa]$R

#-×-coding:gb2312-*-
8P @0P p0pE0import httplib,urllib,urlparse51Testing软件测试网'Yg*mu%w QW
from sgmllib import SGMLParser51Testing软件测试网b2SF'GY2P
#解析指定的网页的html,得到该页面的超链接列表
&S7?Q;Y9__0class URLLister(SGMLParser):51Testing软件测试网c-kj7i)uC+|v
def reset(self):
5uz4}I+s0SGMLParser.reset(self)
SSbQ@n0self.urls = []51Testing软件测试网&sW6TFm$J

\7~m\H [ G9M0def start_a(self, attrs):
4JoX8k5t h:Cw0href = [v for k, v in attrs if k=='href']
a ^|2`k9T&K/U0if href:51Testing软件测试网~}2}_/eD2Q s$U
self.urls.extend(href)51Testing软件测试网2e nI9Qj
#遍历超链接列表,并逐个的发送请求,判断接收后的代码,200为正常,其他为不正常51Testing软件测试网&? ^#lmy"c8iR
def fetch(host):
B:j.I[/w(na4k0usock = urllib.urlopen(host)
V-I zeiS*^L%E4n5n0parser = URLLister()51Testing软件测试网 bCi$~5~&~oX
parser.feed(usock.read())51Testing软件测试网)^ fZo.U9tG2L#Y}
uhost = urlparse.urlparse(host)
eF Gs D2e9w7ac4d9A0for url in parser.urls:
y"po5se+q|0up = urlparse.urlparse(url)51Testing软件测试网!{Jm6rzy
#因为超链接有两种方式:一种是直接的http://...... 一种是相对路径,/.../sample.html
*O8D8Y o'k`C0#所以要分别处理
*V)D@'j1WSpK0if up.netloc =="":51Testing软件测试网K2@FJH3\y9k:c
http = httplib.HTTP(uhost.netloc)
|N^c"LBU0http.putrequest("GET", "/"+up.path+"?"+up.params+up.query+up.fragment)
"od_@wS%`!N0http.putheader("Accept", "*/*")51Testing软件测试网-_B'f9x P
http.endheaders()
%m|,o {0m1aB0else:51Testing软件测试网 G]#B8kEb)dSJ
http = httplib.HTTP(up.netloc)
Ns6ZOr'V(n0http.putrequest("GET", up.path+"?"+up.params+up.query+up.fragment)51Testing软件测试网$t3N)?%v+m+PF]
http.putheader("Accept", "*/*")
%^ lR6E-u$Ul/F^0http.endheaders()51Testing软件测试网m)K1Zml+H;]:k8S2e
errcode, errmsg, headers = http.getreply()51Testing软件测试网hr5M [ DDE1{D
if errcode == 200:51Testing软件测试网&Sp,HT:b6P&Vaq
print url," : ok"51Testing软件测试网(B4}2F(PF2`C
else:51Testing软件测试网"K&c t0uhha8n
print url," : ",errcode51Testing软件测试网%h-u _*J+h5AW

+f [8DN4W|/r'^0#测试
S E5waqt2TG$pt0fetch("http://localhost/Sample/sample.html")51Testing软件测试网]4J o'l*Q)N

A+fi4O\/n S2`0代码运行的结果:51Testing软件测试网E3@tA!v\fr

d]t/cUG W0http://ggdfgdfg.com/erwerwe.html: 404
A[ j'g$H0/sample/lik.html : ok51Testing软件测试网7},h ~9kE9{
/sample/lik2.html : 404
C*rc[0z#N;d0http://google.com: ok

QD{Z,Kl1U051Testing软件测试网n5SwuK'e8g

51Testing软件测试网2la'cgR#n-Xj's

51Testing软件测试网` C#},K.O

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

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


TAG:

 

评分:0

我来说两句

Open Toolbar