0

How can I get only site.com from search results using python to get insights about words in 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
3
  • Did that solve your problem? Commented Apr 18, 2014 at 16:20
  • @sshashank124 No , I am newbie in Python so I am figuring out how to implement the answers into the code I found but no luck till now Commented Apr 18, 2014 at 17:09
  • it works with urlparse . Commented Apr 18, 2014 at 17:36

2 Answers 2

2

You don't need a regex for that, use urlparse

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

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

Sign up to request clarification or add additional context in comments.

Comments

0

Try it with regex as follows:

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

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

If you have a more general site, you can do:

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.