如何在python中验证LinkedIn公共配置文件url正则表达式

2024-10-02 18:15:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我只是想验证linkedin的公共档案网址。我试过下面的概念

 a = "https://in.linkedin.com/afadasdf"
 p = re.compile('(http(s?)://|[a-zA-Z0-9\-]+\.|[linkedin])[linkedin/~\-]+\.[a-zA-Z0-9/~\-_,&=\?\.;]+[^\.,\s<]')
 p.match(a)

上述概念运行良好。但是当我给出urlhttps://www.linkedin.com时,意味着它不起作用。谁能帮我验证这两个概念吗。在


Tags: inhttpsrecomhttp概念match档案
3条回答

这种模式可能会有所帮助。在

^((http|https):\/\/)?+(www.linkedin.com\/)+[a-z]+(\/)+[a-zA-Z0-9-]{5,30}+$

我已经测试过了,对我来说效果很好。在

正是http(s)和www.之间的oring给了您上述问题。您可以将它们更改为*(即0或更多)。在

import re

a = "https://www.linkedin.com/afadasdf"
p = re.compile('((http(s?)://)*([a-zA-Z0-9\-])*\.|[linkedin])[linkedin/~\-]+\.[a-zA-Z0-9/~\-_,&=\?\.;]+[^\.,\s<]')
print p.match(a)

尽管你可能想把它限制在www上,而不是任何数字或字母?所以也许:

^{pr2}$

您可以使用urllib模块,而不是将url与regex匹配:

In [1]: import urllib
In [2]: u = "https://in.linkedin.com/afadasdf"
In [3]: urllib.parse.urlparse(u)
Out[3]: ParseResult(scheme='https', netloc='in.linkedin.com', path='/afadasdf', params='', query='', fragment='')

现在可以检查netlocpath属性。在

相关问题 更多 >