Download Files with zagoload - part 2

Download with params

We can provide GET parameters to the URL using params parameter. In the next example, we will this to query bing search engine for its three top results for "python" and "php". We will rely on the fact that Bing takes a GET parameter named "q" for its search terms and that the results are the text enclosed in cite tag.

sample_02.py
 import re
 import zagoload

 def queryBing(query,count):
   def info(ss):
     import sys ;sys.stdout.write(ss + u'\n')
   def cleanTags(text):
     return re.sub(u'<(.*?)>','',text)
   # download url
   params = {}
   params['q'] = query
   ff = zagoload.load('http://www.bing.com/',params=params)
   if ff.valid:
     info(u'{0} => {1}'.format(ff.source,ff.target))
     info(u'Bing <{1}> - Top {0}'.format(count,query))
     reCite = '<cite>(?P<link>.*?)</cite>'
     zz = 0
     for ii in re.finditer( reCite , ff.text , re.DOTALL ):
       link = cleanTags(ii.group('link'))
       zz += 1
       info(u'  {0} => {1}'.format(zz,link))
       if zz == count:
         break
   else:
     info(u'Failed to download {0}'.format(ff.source))

 queryBing( 'python', 3 )
 queryBing( 'php'   , 3 )
sample_02.py output
 # python sample_02.py
 http://www.bing.com/?q=python => Cache/SS/file_SS9QoJfDt_OuZB6c.bin
 Bing <python> - Top 3
   1 => https://www.python.org
   2 => https://docs.python.org
   3 => https://de.wikipedia.org/wiki/Python_(Programmiersprache)
 http://www.bing.com/?q=php => Cache/09/file_09VHLZwzFwXecVvE.bin
 Bing <php> - Top 3
   1 => ru.php.net
   2 => www.gentlesource.com
   3 => qa.php.net