【问题标题】:Beautiful Soup and URL base names in pythonpython中漂亮的汤和URL基本名称
【发布时间】:2014-02-22 01:11:18
【问题描述】:

你将如何使用 Beautiful soup 在 python 中获取 URL 基名称?给定 url 名称作为字符串,你会怎么做?

【问题讨论】:

    标签: python beautifulsoup base


    【解决方案1】:

    如果你的意思是基本名称,给定http://example.com/file.txt,你想要file.txt?在这种情况下,您根本不需要 Beautiful Soup。简单的字符串操作代码就可以了。

    known 也是os.path.basename('http://example.com/file.txt) 会给你file.txt

    【讨论】:

      【解决方案2】:

      我会在 BeautifulSoup 上使用 urlparse 来提取 URL 片段。这是一个例子:

      from urlparse import urlparse
      
      parsedurl = urlparse('http://example.com/filename.txt')
      print parsedurl.path
      

      输出将是:

      /文件名.txt

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-05-08
        • 1970-01-01
        • 2017-08-24
        • 1970-01-01
        • 2013-03-21
        • 2013-11-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多