File: _web_example3.py

package info (click to toggle)
nodebox-web 1.9.4.6-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,904 kB
  • ctags: 1,602
  • sloc: python: 7,582; ansic: 581; xml: 239; makefile: 2
file content (28 lines) | stat: -rw-r--r-- 704 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Querying Yahoo!

try:
    web = ximport("web")
except:
    web = ximport("__init__")
    reload(web)

# Get a list of links for a search query.
links = web.yahoo.search_images("food")
print links

# Retrieve a random image.
img = web.url.retrieve(choice(links))

# We can't always trust the validity of data from the web,
# the site may be down, the image removed, etc.
# If you're going to do a lot of batch operations and
# you don't want the script to halt on an error,
# put your code inside a try/except statement.
try:
    image(None, 0, 0, data=img.data)
except:
    print str(img.error)
    
# An easier command is web.download():
img = web.download(choice(links))
image(None, 0, 200, data=img)