Skip to content

Instantly share code, notes, and snippets.

@chrisguitarguy
Created October 17, 2011 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisguitarguy/1292953 to your computer and use it in GitHub Desktop.
Save chrisguitarguy/1292953 to your computer and use it in GitHub Desktop.
Trying out the requests library for Python
import requests
from BeautifulSoup import BeautifulSoup as Soup
def get_products(url):
r = requests.get(url)
if 200 != r.status_code:
return False
s = Soup(r.content)
products = s.findAll('li', {'class': 'item'})
out = []
for p in products:
try:
out.append(p.find('a')['href'])
except:
continue
return out
if __name__ == '__main__':
from sys import argv, exit
if len(argv) < 3:
print "Usage: python %s url_to_check output_file.txt" % argv[0]
exit()
else:
products = get_products(argv[1])
if not products:
print repr(products)
exit()
else:
out = open(argv[2], 'w')
for p in products:
out.write('%s\n' % p)
out.close()
exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment