【问题标题】:Extract the last part of a facebook page url提取 facebook 页面 url 的最后一部分
【发布时间】:2016-06-30 15:14:51
【问题描述】:

假设我们有不同的 Facebook 页面链接。我想在这些链接中提取“实体”。例如:

http://www.facebook.com/pages/Blue-Mountain-Aromatics/561694213861926 我想提取'Blue-Mountain-Aromatics'。

http://www.facebook.com/1905BocaJuniors 我想提取'1905BocaJuniors'。

https://www.facebook.com/7upGuatemala?ref=br_tf我想提取'7upGuatemala'

http://www.fb.com/supligenjm我想提取'supligenjm'

http://www.facebook.com/axebolivia?sk=wall&filter=1我想提取'axebolivia'

我尝试了许多 if-else 语句来阻止它,但最终它只是意大利面条代码。

有什么帮助吗?

【问题讨论】:

  • 你能发布一些你已经创建的代码吗?

标签: python regex url entity


【解决方案1】:
try:
    from urlparse import urlparse
except ImportError:
    from urllib.parse import urlparse

links = [
    'http://www.facebook.com/pages/Blue-Mountain-Aromatics/561694213861926',
    'http://www.facebook.com/1905BocaJuniors',
    'https://www.facebook.com/7upGuatemala?ref=br_tf',
    'http://www.fb.com/supligenjm',
    'http://www.facebook.com/axebolivia?sk=wall&filter=1',
]


for url in links:
    url = urlparse(url)
    path = url.path.split('/')
    entity = path[2] if path[1] == 'pages' else path[1]
    print(entity)

【讨论】:

    【解决方案2】:

    @Robᵩs 答案的 Python 3 版本(并重写为函数):

    from urllib.parse import urlparse
    
    links = [
        'http://www.facebook.com/pages/Blue-Mountain-Aromatics/561694213861926',
        'http://www.facebook.com/1905BocaJuniors',
        'https://www.facebook.com/7upGuatemala?ref=br_tf',
        'http://www.fb.com/supligenjm',
        'http://www.facebook.com/axebolivia?sk=wall&filter=1',
    ]
    
    def fb_extract(url):
        url = urlparse(url)
        path = url.path.split('/')
        entity = path[2] if path[1] == 'pages' else path[1]
        return entity
    
    for url in links:
        fb_extract(url)
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-27
      • 2023-03-23
      • 2013-06-14
      • 2011-11-15
      • 2013-11-01
      • 2019-04-30
      相关资源
      最近更新 更多