[python脚本]一个简单的web爬虫(1)

        个人简单的写了个爬虫,可以爬页面链接和多媒体链接,当然这个只适用于一般的网站,没啥技术含量,纯属练手只用·········

         不过以后我还会在改进的。现在而且只能爬单个页面,呵呵·······

         python确实简单,20几行的代码 就可以解决(我这代码27行以后都没用,纯属自己弄着完的)

 

 1 #filename:Spider1.py
 2 #version:1.0
 3 #--coding: utf-8--
 4 #author: Scr@t
 5 
 6 import sys
 7 import urllib2
 8 import re
 9 import HTMLParser
10 
11 class myparser(HTMLParser.HTMLParser):
12     def __init__(self):
13         HTMLParser.HTMLParser.__init__(self)
14     def handle_starttag(self,tag,attrs):
15         if (tag == 'a')|(tag == 'img'):#查询标签是否为网址链接或多媒体链接
16             for name,value in attrs:
17                 if (name == 'href')|(name == 'src'):#查询该上面两个标签的属性
18                     val = re.search('http://',value)#匹配链接是否为可用链接(有的时候会有空链接的)
19                     if val != None:
20                          print value     
21                           
22 if sys.argv[1] == '-u':
23     content = (urllib2.urlopen(sys.argv[2])).read()#打开网址并读取内容
24     con = myparser()
25     con.feed(content)#把content的内容,传给myparser分析
26 else:
27     print 'Usage:%s -u url'%sys.argv[0] 
28 print """                                                                                                                       
29 -------------------------------------------------------------------------------------------                                                                                                                      
30   |        **        **        **   ******************   *****************              |
31   |         **      ****      **    **                   **               *             |
32   |          **    **  **    **     ****************     *****************              |
33   |           **  **    **  **      **                   **               *             |
34   |            ****      ****       **                   **               *             |
35   |             **        **        ******************   *****************              |
36   |                                                                                     |
37   |    *****     **********   **********  ***********    *************  ************    |
38   |  ********   **        **      **      **        **   **             **         **   |
39   |   **    **  **        **      **      **         **  **             **         **   |
40   |    **       **        **      **      **          ** **             **         **   |
41   |     ***     ***********       **      **          ** *************  ************    |
42   |       **    **                **      **          ** **             ** **           |
43   | **     **   **                **      **         **  **             **    **        |
44   |  ********   **                **      **        **   **             **      **      |
45   |   ******    **            **********  ***********    *************  **        **    |
46   |                                                                                     |
47   |              author:scr@t                              version: 1.0                 |
48   |                                                                                     |
49 -------------------------------------------------------------------------------------------

50 """   

          好了下面给张图片·················爬的百度首页··········

 

posted @ 2012-07-17 16:03  xss  阅读(6000)  评论(1编辑  收藏