如何使用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
答案 0 :(得分:2)
您不需要正则表达式,请使用urlparse
hostname = urlparse.urlparse("http://www.techcrunch.com/").hostname
答案 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