0

有谁知道修复“损坏”网址的库。当我尝试打开一个网址时,例如

http://www.domain.com/../page.html
http://www.domain.com//page.html
http://www.domain.com/page.html#stuff

urllib2.urlopen 阻塞并给我一个 HTTPError 回溯。有谁知道可以解决这些问题的图书馆?

4

1 回答 1

1

像...这样的东西呢:

import re
import urlparse

urls = '''
http://www.domain.com/../page.html
http://www.domain.com//page.html
http://www.domain.com/page.html#stuff
'''.split()

def main():
  for u in urls:
    pieces = list(urlparse.urlparse(u))
    pieces[2] = re.sub(r'^[./]*', '/', pieces[2])
    pieces[-1] = ''
    print urlparse.urlunparse(pieces)

main()

如您所愿,它确实会发出:

http://www.domain.com/page.html
http://www.domain.com/page.html
http://www.domain.com/page.html

如果我理解正确的话,似乎大致符合您的需求。

于 2009-09-17T03:10:31.530 回答