免费注册 查看新帖 |

Chinaunix

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

python中怎样从文本中识别链接? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-09-04 11:15 |只看该作者 |倒序浏览
python中怎样从文本中识别链接?
用正泽表达式可以么

论坛徽章:
0
2 [报告]
发表于 2006-09-04 14:03 |只看该作者
可以用正则表达式

论坛徽章:
0
3 [报告]
发表于 2006-09-04 15:40 |只看该作者
楼上能给各方式么?我刚学python不久

论坛徽章:
0
4 [报告]
发表于 2006-09-04 16:19 |只看该作者
re.compile(r'\b(http|ftp)://(.*?)(\s|$)', re.I)

论坛徽章:
0
5 [报告]
发表于 2006-09-05 08:17 |只看该作者
原帖由 limodou 于 2006-9-4 16:19 发表
re.compile(r'\b(http|ftp)://(.*?)(\s|$)', re.I)


哈哈,你跑到这来了啊,
我一会去试试

知道我是谁了吧?嘿嘿

论坛徽章:
0
6 [报告]
发表于 2006-09-05 09:02 |只看该作者
不知道你是谁。

论坛徽章:
0
7 [报告]
发表于 2006-09-05 09:47 |只看该作者

  1. from sgmllib import SGMLParser

  2. class URLLister(SGMLParser):
  3.         def reset(self):
  4.                 SGMLParser.reset(self)
  5.                 self.urls = []

  6.         def start_a(self, attrs):
  7.                 href = [v for k, v in attrs if k=='href']
  8.                 if href:
  9.                         self.urls.extend(href)

  10. if __name__ == "__main__":
  11.         import urllib
  12.         usock = urllib.urlopen("http://diveintopython.org/")
  13.         parser = URLLister()
  14.         parser.feed(usock.read())
  15.         parser.close()
  16.         usock.close()
  17.         for url in parser.urls: print url
复制代码

论坛徽章:
0
8 [报告]
发表于 2006-09-05 10:22 |只看该作者
原帖由 yarshure 于 2006-9-5 09:47 发表
[code]
from sgmllib import SGMLParser

class URLLister(SGMLParser):
        def reset(self):
                SGMLParser.reset(self)
                self.urls = []

        def start_a(self, attrs):
                href = [v for k, v in attrs if ...


我按你方式用了一下,好像结果不太对
# -*- coding: cp936 -*-
import urllib
import urllib2

dress=raw_input("请输入 url 地址 : ")

proxyhost = 'http://14.0.189.69:808'
print dress
if proxyhost:
        print "\nProxy is: %s" % proxyhost
        proxy=urllib2.ProxyHandler({'http':proxyhost})
        opener=urllib2.build_opener(proxy)
        urllib2.install_opener(opener)

from sgmllib import SGMLParser

class URLLister(SGMLParser):
        def reset(self):
                SGMLParser.reset(self)
                self.urls = []

        def start_a(self, attrs):
                href = [v for k, v in attrs if k=='href']
                if href:
                        self.urls.extend(href)

if __name__ == "__main__":
        
        usock = urllib.urlopen(dress)
        parser = URLLister()
        parser.feed(usock.read())
        parser.close()
        usock.close()
        for url in parser.urls: print url


例如输入dress :http://www.ziling.com/bbs/dispbb ... D=475142&page=1

打印结果为
http://www.ziling.com/ad.htm
boardhelp.asp?boardID=26
http://bbs.ziling.com/
index.asp?boardid=35
index.asp?boardid=41
。。。。。。中间省略
how.asp?filetype=3&boardid=26
show.asp?filetype=4&boardid=26
show.asp
http://www.dvbbs.net/
http://www.dvbbs.net/download.asp

其中没有我最关心的带.wmv的链接啊?

论坛徽章:
0
9 [报告]
发表于 2006-09-05 10:24 |只看该作者
原帖由 limodou 于 2006-9-5 09:02 发表
不知道你是谁。


我用了你的方式,打印出一个列表
[('http', 'www.w3.org/TR/html4/loose.dtd">', '\n'), ('http', 'www.ziling.com/ad.htm"', ' '), ('http', 'bbs.ziling.com/"><img', ' '), ('http', 'www.ziling.com/img/zlbbs.gif"', ' '), ('http', 'download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0"', ' '), ('http', 'www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"', ' '), ('http', 'bbs.ziling.com/"', ' '), ('http', 'bbs.ziling.com/</div></div>', '\n'), ('http', 'ac2.gameage.net/ad/ad_06090111.wmv"/><embed', ' '), ('http', 'activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701"', ' '), ('http', 'ac2.gameage.net/ad/ad_06090111.wmv"', ' '), ('http', 'www.10203.com"', ' '), ('http', 'www.dvbbs.net/"', ' '), ('http', 'www.dvbbs.net/download.asp"', ' '), ('http', 'download.alexa.com/?p=TrafficDet_W_t_40_L1"', ' '), ('http', 'www.alexa.com/data/details/traffic_details?&y=t&q=&url=http://www.ziling.com"', ' '), ('http', 'www.ziling.com/ad.htm"', ' '), ('http', 'www.ziling.com/2005/1-14/20180659983.html"', ' '), ('http', 'www.ziling.com/2005/1-14/20303759425.html"', ' '), ('http', 'www.google-analytics.com/urchin.js"', ' ')]

好像不太对劲呀

论坛徽章:
0
10 [报告]
发表于 2006-09-05 11:56 |只看该作者
你不是说文本文件嘛,怎么改成html文件了?

上面SGMLParser的代码是把所有的<a href="xxx">中的xxx取出来。而我给的就是从文本中匹配链接取出来,处理方式不同,不知道你到底要什么样的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP