Python regexp获取不带http的url

2024-09-28 23:24:12 发布

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

我怎么能只得到site.com网站使用python从搜索结果中获得关于googleseach中单词的见解?在

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

Tags: fromcomgs网站pagesiteres单词
2条回答

使用regex尝试如下:

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

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

如果您有一个更一般的网站,您可以:

^{pr2}$

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

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

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

相关问题 更多 >