51Testing软件测试网R*d MW4ryHVR 在web测试,对网页中的超链接进行测试是最基本的工作,最简便的方法当然是使用像xenu之类的工具。但它具体是怎么实现的呢?我想也无外乎是通过http协议,根据超链接地址,向服务端发送请求,然后根据返回的信息进行判断连接的状态。下面是根据这种思路,用python编写的检测网页链接连通性的程序。51Testing软件测试网X T]$jd+{u7a
51Testing软件测试网
Z{!f;FU%z"G}ka首先,建立一个示例网页,其中link1,lin3是不连通的,link2,link4是有效链接
7` p J.Qu3[0H rMY?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}0VZH'BT8pw,q,Kz@0sgmllib :用于HTML解析,解析出网页中包含的超链接
(lL"b"ggj051Testing软件测试网V-H:kt8D!V0w!Khttplib:用于Http协议的操作
4h2fhY#?]051Testing软件测试网#|s$gMG0[urllib:用于获取网页的html代码51Testing软件测试网)_%hK|$k A
51Testing软件测试网JyeA){7rb(dEurlparse:解析url地址,把url地址解析成几个部分。
$q/M+{7`0[dT0clT:K
Rh0具体实现代码如下:51Testing软件测试网xL/Tn@J*wi5a,ER
Uq4q1xozIy0#-×-coding:gb2312-*-51Testing软件测试网s/|ELI%BJ*R)P
import httplib,urllib,urlparse51Testing软件测试网Q4r'n U"?B
from sgmllib import SGMLParser51Testing软件测试网]r eF
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)L H"O8XqR
q`k
else:
~Q2p,M:fg(tG0_
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]JL
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}0g;Q`+b\yq0#测试51Testing软件测试网j5? UR'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
G2ILM!k1U1Kl0/sample/lik2.html : 40451Testing软件测试网%kHB9Gk9V
A_m
http://google.com : ok51Testing软件测试网ja*e+w;t sb3V.Yp
M#Z;bk'dT"i0 51Testing软件测试网:RCsb7z6Y)M2h
GQ%Y%sO0版权声明:本文出自wxf_xsfy的51Testing软件测试博客:http://www.51testing.com/?61753
原创作品,转载时请务必以超链接形式标明文章原始出处、作者信息和本声明,否则将追究法律责任。
8v!Fs]2U(U0
U~8NlEl_(|0