免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 9477 | 回复: 0
打印 上一主题 下一主题

python获取网站外链 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-11-02 14:49 |只看该作者 |倒序浏览
转自:http://www.cnpythoner.com/post/122.html
  1. #encoding=utf-8
  2. #@author:老王python
  3. #@description:检查网站的外链

  4. import urllib
  5. from sgmllib import SGMLParser
  6. import re
  7. import sys

  8. infolist = {}#结果列表

  9. class LinkParser(SGMLParser):
  10.     '''抓取link列表的信息'''
  11.     def reset(self):
  12.         SGMLParser.reset(self)
  13.         self.url = ''#链接文本
  14.         self.li_check = 0
  15.         self.a_check = 0
  16.         self.jt_url = ''#具体url
  17.         self.infolist = {}


  18.     def start_li(self, tag):
  19.         '''检测li开头'''
  20.         if tag:
  21.             if tag[0][1] == 'sco5li0' or tag[0][1] == 'sco5li1':
  22.                     self.li_check = 1

  23.     def start_a(self,tag):
  24.         '''检测a开头'''
  25.         if self.li_check == 1:
  26.             if not tag[0][1].startswith('http://203.209.253.250'):
  27.                 host_re  = re.compile(r'^https?://(.*?)($|/)',
  28.                     re.IGNORECASE
  29.                 )               
  30.                 self.url = host_re.search(tag[0][1]).group(1)
  31.                 self.jt_url = tag[0][1]
  32.                 self.a_check = 1


  33.     def handle_data(self, text):
  34.         '''处理空白文本'''
  35.         txt = text.strip()

  36.         if txt and self.a_check and txt != '快照':
  37.             checkurl = '%s,%s' % (self.url,self.jt_url)
  38.             self.infolist[checkurl] = txt
  39.             
  40.         if txt == '':
  41.             return
  42.         
  43.     def end_li(self):
  44.         self.li_check = 0
  45.         
  46.     def end_a(self):
  47.         self.a_check = 0


  48. numre = re.compile(r'<strong>.+')#匹配总的记录数
  49. pnum = re.compile(r'\d+')

  50. checkurl = ''#查询网站的地址,比如http://www.xxx.com
  51. checkurl = urllib.quote(checkurl)#请求地址

  52. pageurl = 'http://sitemap.cn.yahoo.com/search?bwm=i&bwmo=d&p=%s' % (checkurl)
  53. content = urllib.urlopen(pageurl).read()
  54. c = numre.search(content).group(0)
  55. totalnum =  int(pnum.search(c).group(0))#总的外链数


  56. host_re  = re.compile(r'^http://(?P<host>www\.(?:[A-Z0-9-]+\.){1}[A-Z\.]{2,6}),
  57.     re.IGNORECASE
  58. )

  59. pagesize = 50#一页显示50条

  60. if totalnum % pagesize:
  61.     page = totalnum/pagesize
  62. else:
  63.     page = (totalnum/pagesize)+1


  64. f = file('a.txt','w')

  65. for k in xrange(page):
  66.     parser = LinkParser()
  67.     url = 'http://sitemap.cn.yahoo.com/search?bwm=i&bwmo=d&p=%s&b=%s' % (checkurl,k*50)
  68.     print 'url=========>',url
  69.     cstr = urllib.urlopen(url).read()
  70.     parser.feed(cstr)
  71.     parser.close()
  72.    
  73.     for m in parser.infolist:
  74.         domain,jt_url = m.split(',')
  75.         print 'domain--------->',domain
  76.         print 'jt_url--------->',jt_url
  77.         t =  'url:%s,jt_url:%s,title:%s\n' % (domain,jt_url,parser.infolist[m].decode('utf-8').encode('utf-8'))
  78.         f.write(t)
  79. f.close()
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP