Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python urlparse.urljoin()不处理无效的父目录_Python_Url_Screen Scraping_Relative Url - Fatal编程技术网

Python urlparse.urljoin()不处理无效的父目录

Python urlparse.urljoin()不处理无效的父目录,python,url,screen-scraping,relative-url,Python,Url,Screen Scraping,Relative Url,当从相对URL构建绝对URL时,是否有方法解释“无效”父目录,或者我应该只使用.replace() 更好的是,在Python中进行抓取时,有没有更干净的方法来清理URL?正如您所说,这没有意义。您可以从根目录转到更高的位置。因此,在不了解作者意图的情况下,将第二部分规范化将是困难的。只有你知道如何正确消毒。:) 请参阅以获得解决方案。 >>> from urlparse import urljoin >>> url = urljoin('http://www.

当从相对URL构建绝对URL时,是否有方法解释“无效”父目录,或者我应该只使用
.replace()


更好的是,在Python中进行抓取时,有没有更干净的方法来清理URL?

正如您所说,这没有意义。您可以从根目录转到更高的位置。因此,在不了解作者意图的情况下,将第二部分规范化将是困难的。只有你知道如何正确消毒。:)

请参阅以获得解决方案。
>>> from urlparse import urljoin
>>> url = urljoin('http://www.example.com/path/', '../../../index.html')
>>> url
'http://www.example.com/../../index.html'
>>> url.replace('../', '')
'http://www.example.com/index.html'