Python正则表达式获取没有http或www的URL

时间:2014-04-18 16:09:31

标签: python regex

如何使用python从搜索结果中获取site.com以获取有关google seach中的字词的见解?

from xgoogle.search import GoogleSearch, SearchError
try:
  page = 1
  gs = GoogleSearch("#hashtag insights")
  gs.results_per_page = 100
  results = []
  while True:
    tmp = gs.get_results()
    if not tmp: # no more results were found
      break
    results.extend(tmp)
  # ... do something with all the results ...
except SearchError, e:
  print "Search failed: %s" % e

for res in results:
    print res.url

2 个答案:

答案 0 :(得分:2)

您不需要正则表达式,请使用urlparse

hostname = urlparse.urlparse("http://www.techcrunch.com/").hostname

http://docs.python.org/library/urlparse.html

答案 1 :(得分:0)

使用正则表达式尝试如下:

import re
s = 'http://www.google.com'

>>> print re.search(r'^https?:\/\/www\.(.*)$', s).group(1)
google.com

如果您有更一般的网站,可以这样做:

import re
s = 'http://username.blogspot.com'

>>> print re.search(r'^https?:\/\/[^.]*.(.*)$', s).group(1)
blogspot.com