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 29 30 31 32
|
#!/usr/bin/env python3
# Search PyPI, the Python Package Index, and retrieve latest mechanize tarball.
# This is just to demonstrate mechanize: You should use easy_install to do
# this, not this silly script.
import sys
import os
import mechanize
from mechanize.polyglot import urlsplit
def download_mechanize():
browser = mechanize.Browser(factory=mechanize.RobustFactory())
browser.set_handle_robots(False)
browser.open("http://pypi.python.org/pypi")
browser.select_form(name="searchform")
browser.form["term"] = "mechanize"
browser.submit()
browser.follow_link(text_regex="mechanize-?(.*)")
link = browser.find_link(text_regex=r"\.tar\.gz")
filename = os.path.basename(urlsplit(link.url)[2])
if os.path.exists(filename):
sys.exit("%s already exists, not grabbing" % filename)
browser.retrieve(link.url, filename)
if __name__ == "__main__":
download_mechanize()
|