Created
November 26, 2012 19:43
-
-
Save nloadholtes/4150184 to your computer and use it in GitHub Desktop.
Playing around to figure out an API
This file contains hidden or 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
# | |
# script for getting data from pic2shop.com | |
# Nick Loadholtes <[email protected]> | |
# | |
# Nov 26, 2012 | |
# | |
import csv | |
import sys | |
import random | |
import time | |
import urllib2 | |
def main(listing): | |
output = [] | |
with open(listing, 'r') as csvfile: | |
rows = csv.DictReader(csvfile) | |
for row in rows: | |
output.append(row['ISBN13']) | |
return output | |
URL = """http://http://www.pic2shop.com/item/%s_0""" | |
PATH = """output/%s.html""" | |
def getListing(isbn): | |
url = URL % isbn | |
req = urllib2.Request(url) | |
try: | |
response = urllib2.urlopen(req) | |
data = response.read() | |
fullpath = PATH % isbn | |
f = open(fullpath, 'w') | |
f.write(data) | |
f.close() | |
except: | |
print("Failed to get %s" % url) | |
if __name__ == '__main__': | |
if len(sys.argv) < 2: | |
print("Need a csv file to scan") | |
exit(-1) | |
print(len(sys.argv)) | |
books = main(sys.argv[1]) | |
for isbn in books: | |
getListing(isbn) | |
time.sleep(random.random() + 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment