Guest User

Untitled

a guest
Apr 23rd, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. #!/usr/bin/python
  2. import json
  3. import urllib
  4.  
  5. def showsome(searchfor):
  6. query = urllib.urlencode({'q': searchfor})
  7. url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s' % query
  8. search_response = urllib.urlopen(url)
  9. search_results = search_response.read()
  10. results = json.loads(search_results)
  11. data = results['responseData']
  12. print 'Total results: %s' % data['cursor']['estimatedResultCount']
  13. hits = data['results']
  14. print 'Top %d hits:' % len(hits)
  15. for h in hits: print ' ', h['url']
  16. print 'For more results, see %s' % data['cursor']['moreResultsUrl']
  17.  
  18. showsome('ermanno olmi')
  19.  
  20. #!/usr/bin/python3
  21. import json
  22. import urllib.request, urllib.parse
  23.  
  24. def showsome(searchfor):
  25. query = urllib.parse.urlencode({'q': searchfor})
  26. url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s' % query
  27. search_response = urllib.request.urlopen(url)
  28. search_results = search_response.read().decode("utf8")
  29. results = json.loads(search_results)
  30. data = results['responseData']
  31. print('Total results: %s' % data['cursor']['estimatedResultCount'])
  32. hits = data['results']
  33. print('Top %d hits:' % len(hits))
  34. for h in hits: print(' ', h['url'])
  35. print('For more results, see %s' % data['cursor']['moreResultsUrl'])
  36.  
  37. showsome('ermanno olmi')
  38.  
  39. # Get the first 20 hits for: "Breaking Code" WordPress blog
  40. from google import search
  41. for url in search('"Breaking Code" WordPress blog', stop=20):
  42. print(url)
  43.  
  44. # Get the first 20 hits for "Mariposa botnet" in Google Spain
  45. from google import search
  46. for url in search('Mariposa botnet', tld='es', lang='es', stop=20):
  47. print(url)
  48.  
  49. import urllib2
  50.  
  51. def getgoogleurl(search,siteurl=False):
  52. if siteurl==False:
  53. return 'http://www.google.com/search?q='+urllib2.quote(search)
  54. else:
  55. return 'http://www.google.com/search?q=site:'+urllib2.quote(siteurl)+'%20'+urllib2.quote(search)
  56.  
  57. def getgooglelinks(search,siteurl=False):
  58. #google returns 403 without user agent
  59. headers = {'User-agent':'Mozilla/11.0'}
  60. req = urllib2.Request(getgoogleurl(search,siteurl),None,headers)
  61. site = urllib2.urlopen(req)
  62. data = site.read()
  63. site.close()
  64.  
  65. #no beatifulsoup because google html is generated with javascript
  66. start = data.find('<div id="res">')
  67. end = data.find('<div id="foot">')
  68. if data[start:end]=='':
  69. #error, no links to find
  70. return False
  71. else:
  72. links =[]
  73. data = data[start:end]
  74. start = 0
  75. end = 0
  76. while start>-1 and end>-1:
  77. #get only results of the provided site
  78. if siteurl==False:
  79. start = data.find('<a href="/url?q=')
  80. else:
  81. start = data.find('<a href="/url?q='+str(siteurl))
  82. data = data[start+len('<a href="/url?q='):]
  83. end = data.find('&sa=U&ei=')
  84. if start>-1 and end>-1:
  85. link = urllib2.unquote(data[0:end])
  86. data = data[end:len(data)]
  87. if link.find('http')==0:
  88. links.append(link)
  89. return links
  90.  
  91. links = getgooglelinks('python','http://www.stackoverflow.com/')
  92. for link in links:
  93. print link
  94.  
  95. from lib.google_search_results import GoogleSearchResults
  96.  
  97. params = {
  98. "q" : "Coffee",
  99. "location" : "Austin, Texas, United States",
  100. "hl" : "en",
  101. "gl" : "us",
  102. "google_domain" : "google.com",
  103. "api_key" : "demo",
  104. }
  105.  
  106. query = GoogleSearchResults(params)
  107. dictionary_results = query.get_dictionary()
Add Comment
Please, Sign In to add comment