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

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

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

51Testing软件测试网[A:g&vl)UU

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

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

}B#Q&H)I051Testing软件测试网(j2q^yB]

<head>Test</hest>51Testing软件测试网U_PiG+]2|eJ"_
<body>51Testing软件测试网 g;J|)k$jB#Sm7w
 <a href="http://ggdfgdfg.com/erwerwe.html">link1</a>
A I*wK;rq`Y t0 <a href="/sample/lik.html">link2</a>51Testing软件测试网Rk#v!S9z
 <a href="/sample/lik2.html">link3</a>
$?2\,xgJM0 <a href="http://google.com">link4</a>51Testing软件测试网;}.u$P|.U^ `\.U
</body>51Testing软件测试网O&? K/Iy&t1?*[%`G0k

51Testing软件测试网8q'M/L%]+P,C

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

.r$t`'O4EL,u051Testing软件测试网T^W!c^O"|,ZT8n

sgmllib :用于HTML解析,解析出网页中包含的超链接51Testing软件测试网5?0G&fL%pG-{+pT#R

'qjv!}C L9_hV0httplib:用于Http协议的操作

)pp*InQ2E9E,M0

A&C:d?'a}W0urllib:用于获取网页的html代码51Testing软件测试网9c G,Cp5iU,f6n

51Testing软件测试网IX+q!ae w

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

9u3wtwvp6Qg L#q0

r]0AE+i p2T0具体实现代码如下:51Testing软件测试网K@FN8X H2~)yk7~0?ad

Nz!f T5h[^+n0#-×-coding:gb2312-*-51Testing软件测试网7x Yo R7K)_ R&l
import httplib,urllib,urlparse
"nq_~+\_0from sgmllib import SGMLParser
r1@A&ykX0Q0#解析指定的网页的html,得到该页面的超链接列表51Testing软件测试网(efn8{`D
class URLLister(SGMLParser):
%dd!`1Cm x"|0    def reset(self):
];m;{8l5^-zD|1x2k_0        SGMLParser.reset(self)
*u)h/I's/? avvy0        self.urls = []

2m*iZ!? n:j0

nOA)L}q\AW0    def start_a(self, attrs):51Testing软件测试网1L5k}&s7E6aS+p+nb
        href = [v for k, v in attrs if k=='href']
#g&O*q,v7P&^ v2H0        if href:51Testing软件测试网~Qm+D&D
            self.urls.extend(href)
1_?&^(\ y.bA#r gK0#遍历超链接列表,并逐个的发送请求,判断接收后的代码,200为正常,其他为不正常51Testing软件测试网fS%f&j7d+V,Dr FiB
def fetch(host):51Testing软件测试网:R o:B| zz+f%Q
        usock = urllib.urlopen(host)51Testing软件测试网QPD^`#[,f#V;J9Z
        parser = URLLister()51Testing软件测试网a8g)|.P$O*] R+y
        parser.feed(usock.read())51Testing软件测试网4P OK.|'L2[$J-L&q U
        uhost = urlparse.urlparse(host)51Testing软件测试网$JRx$n1ec
        for url in parser.urls:51Testing软件测试网N{ M0r^&U;Lj
            up = urlparse.urlparse(url)51Testing软件测试网X(EJ~^3v^K0e
            #因为超链接有两种方式:一种是直接的http://...... 一种是相对路径,/.../sample.html
Z&dr+t!I0          #所以要分别处理51Testing软件测试网Z(v:r(CX2`l
            if up.netloc =="":51Testing软件测试网mK:um1Z lvA3P
                http = httplib.HTTP(uhost.netloc)
hx)Oc6vM.f7w.R%Qn0                http.putrequest("GET", "/"+up.path+"?"+up.params+up.query+up.fragment)51Testing软件测试网PG&ZQE
                http.putheader("Accept", "*/*")51Testing软件测试网T L'o1_~0JPH(v@
                http.endheaders()
&Y Rf2rMFU0            else:51Testing软件测试网:W;l$W W@!A4h*P
                http = httplib.HTTP(up.netloc)
,stW9KQPRN0                http.putrequest("GET", up.path+"?"+up.params+up.query+up.fragment)51Testing软件测试网bl-l3I8Z)di@
                http.putheader("Accept", "*/*")51Testing软件测试网V.@ WA;cK.\~"A
                http.endheaders()
}5A(?Sh-Cs0            errcode, errmsg, headers = http.getreply()51Testing软件测试网w,B"aL9g
            if errcode == 200:51Testing软件测试网[| o;ZL H Mg-zU(Y
                print url," : ok"51Testing软件测试网LUE_l k{4Q(e
            else:
C N'D?'?&F:e0                print url," : ",errcode51Testing软件测试网oqJ;[,S2KJ

1z t$F }!Wr:S+[~0#测试51Testing软件测试网&sfd6H6w^n`
fetch("http://localhost/Sample/sample.html")51Testing软件测试网#reW(\S0z

&K v&f+}5V$hh7c0代码运行的结果:51Testing软件测试网u'Ud?'U)~

AS'D}r kU/D&eO0http://ggdfgdfg.com/erwerwe.html  :  40451Testing软件测试网[.NP%b c
/sample/lik.html  : ok51Testing软件测试网2m x5OU1a-K2B h&T
/sample/lik2.html  :  40451Testing软件测试网F9S5E;ExGe\e
http://google.com  : ok

w XxSw g0uy e051Testing软件测试网{Pi xJwC

 51Testing软件测试网3JMT^v+C%v

'oU1X$] b*Z,N0版权声明:本文出自wxf_xsfy的51Testing软件测试博客:http://www.51testing.com/?61753

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

]A*K6[mW2h)T[`!{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-05-05  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

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

RSS订阅

Open Toolbar