I parse the link with the following code:

m = re.search('http\://([^/]*)/?.*', url) host = m.group(1) 

But if you insert a link with https, an error will occur:

 AttributeError: 'NoneType' object has no attribute 'group' 

Is it possible to somehow rewrite the code so that the .search function can accept both http and https at the same time?

  • 2
    in the following way: re.search('http[s]?\://([^/]*)/?.*', url) , but it's better to do this with special modules ... - MaxU
  • one
    I advise you to check if there was a match: m = re.search(...) , if m: host=m.group(1) , otherwise such errors cannot be avoided. Can a single character be optional with a quantifier ? . - Wiktor Stribiżew pm

2 answers 2

Of course available

 m = re.search('https?://([^/]*)/?.*', url) host = m.group(1) 

    You can change the regular expression to this:

     'https?\://([^/]*)/?.*' 

    but since you do not even know the basics of regular expressions, I do not recommend you to use them in real-world problems.
    Python has a module urllib.parse and a function urlparse in it:

     >>> import urllib.parse as urlparse >>> urlparse.urlparse( 'https://google.com/q=' ).netloc 'google.com'