Created
October 17, 2011 16:01
-
-
Save chrisguitarguy/1292953 to your computer and use it in GitHub Desktop.
Trying out the requests library for Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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