Aug 22, 2012 04:42 PM
(This post was last modified: Aug 23, 2012 05:29 PM by antigrav.)
SOURCE CODE AT BOTTOM
Here's my problem. I am attempting to make a program that takes Newegg product URLs (like http://www.newegg.com/Product/Product.as...6813157301) and outputs info about the item. So if I give it the URL in the example, it returns with this:
ASRock H77 Pro4-M LGA 1155 Intel H77 HDMI SATA 6Gb/s USB 3.0 Micro ATX Intel Motherboard
$89.99
http://www.newegg.com/Product/Product.as...6813157301
----
Here's my dilemma. I can do this with one item, no problem. After getting all the variables, it asks you to exit or paste to pastebin (only works if you are logged into pastebin on your default browser). When you paste to pastebin, the program starts over the process. Takes another url, gives more info, etc.
The problem with this is, once it does it more than once, if you want to paste to pastebin, it will make a new paste. I want to make it so it all goes into one paste.
Here's my source code. I'm not worried about people stealing it too much, since this is a personal project (but if you're going to use it in your own program to release to the public, please at least credit me first):
How should I go about solving this?
POSSIBLE SOLUTION
What if I use the old pastebin url, take all the info from it, store it in a new variable called old_product_info, and add that to product_info? If I do more than 2 recursions, I just keep adding to old_product_info.
Possible solution? Testing now.
SOLVED
Added a counter for how many times the program was ran, and if it was over one, it would go to the old info and add it to the new info.
Source code at end;
Here's my problem. I am attempting to make a program that takes Newegg product URLs (like http://www.newegg.com/Product/Product.as...6813157301) and outputs info about the item. So if I give it the URL in the example, it returns with this:
ASRock H77 Pro4-M LGA 1155 Intel H77 HDMI SATA 6Gb/s USB 3.0 Micro ATX Intel Motherboard
$89.99
http://www.newegg.com/Product/Product.as...6813157301
----
Here's my dilemma. I can do this with one item, no problem. After getting all the variables, it asks you to exit or paste to pastebin (only works if you are logged into pastebin on your default browser). When you paste to pastebin, the program starts over the process. Takes another url, gives more info, etc.
The problem with this is, once it does it more than once, if you want to paste to pastebin, it will make a new paste. I want to make it so it all goes into one paste.
Here's my source code. I'm not worried about people stealing it too much, since this is a personal project (but if you're going to use it in your own program to release to the public, please at least credit me first):
Code
# REFORMATTED VERSION OF SOURCECODE FOR PROJECT
# MADE PURELY TO INCREASE READABILITY
import urllib
import urllib2
import webbrowser
import json
import time
import sys
from bs4 import BeautifulSoup
while True:
newegg_URL = raw_input('Enter the URL of the Newegg item: \n')
not_product_code = 'http://www.newegg.com/Product/Product.aspx?Item='
product_code = newegg_URL.replace(not_product_code, '')
product_info_URL = 'http://www.ows.newegg.com/Products.egg/' + product_code
soup = BeautifulSoup(urllib.urlopen(newegg_URL))
name_with_site = soup.title.string
name = name_with_site.replace('Newegg.com - ', '')
print '\n' + name
data = json.load(urllib2.urlopen(product_info_URL))
price = data[u'FinalPrice']
print '\n' + price
print '\n' + newegg_URL
product_info = name + '\n' + price + '\n' + newegg_URL
continue_or_export = raw_input('\n\nAre you done? ' +
'To quit the program, type 1. ' +
'To export to PasteBin, type 2. \n\n')
if continue_or_export == '1':
sys.exit()
elif continue_or_export == '2':
print 'Note: Exporting to pastebin only works if one is logged in on their default browser. Attempting now.'
pastebin_vars = {'api_dev_key':'57fe1369d02477a235057557cbeabaa1',
'api_option':'paste',
'api_paste_code':product_info}
response = urllib.urlopen('http://pastebin.com/api/api_post.php', urllib.urlencode(pastebin_vars))
pastebin_url = response.read()
print pastebin_url
webbrowser.open_new_tab(pastebin_url)
else:
print 'Your input was not understood. Use "1" or "2" only.'
#product_category = 0 <- coming soon
# VARIABLE NAMES + DESCRIPTIONS
# newegg_URL - the url of the newegg product found in url
# soup - the beautiful soup html of the newegg product
# product_code - code of the product
# product_info_URL - url of the page that holds price and rating
# usock - urllib's product info url thing
# data - json interpretation of the page with details on it
# price - product price
# name_with_site - product name
# name - product name, but without the annoying "newegg.com - " part
# product_category - product category COMING SOON SO HARD TO DO D:
# todo: make it take more than one in one instance and make it export to SOMETHING
# (notepad? pastebin? SOMETHING?)How should I go about solving this?
POSSIBLE SOLUTION
What if I use the old pastebin url, take all the info from it, store it in a new variable called old_product_info, and add that to product_info? If I do more than 2 recursions, I just keep adding to old_product_info.
Possible solution? Testing now.
SOLVED
Added a counter for how many times the program was ran, and if it was over one, it would go to the old info and add it to the new info.
Source code at end;
Code
# REFORMATTED VERSION OF SOURCECODE FOR PROJECT
# MADE PURELY TO INCREASE READABILITY
import urllib
import urllib2
import webbrowser
import json
import time
import sys
from bs4 import BeautifulSoup
count = 0
while True:
newegg_URL = raw_input('Enter the URL of the Newegg item: \n')
not_product_code = 'http://www.newegg.com/Product/Product.aspx?Item='
product_code = newegg_URL.replace(not_product_code, '')
product_info_URL = 'http://www.ows.newegg.com/Products.egg/' + product_code
soup = BeautifulSoup(urllib.urlopen(newegg_URL))
name_with_site = soup.title.string
name = name_with_site.replace('Newegg.com - ', '')
print '\n' + name
data = json.load(urllib2.urlopen(product_info_URL))
price = data[u'FinalPrice']
print '\n' + price
print '\n' + newegg_URL
product_info = name + '\n' + price + '\n' + newegg_URL + '\n'
final_product_info = product_info
continue_or_export = raw_input('\n\nAre you done? ' +
'To quit the program, type 1. ' +
'To export to PasteBin, type 2. \n\n')
count+=1
if continue_or_export is '1':
sys.exit()
elif continue_or_export is '2':
if count > 1:
not_pastebin_url_id = 'http://pastebin.com/'
pastebin_url_id = pastebin_url.replace(not_pastebin_url_id, '')
pastebin_raw_url = 'http://pastebin.com/raw.php?i=' + pastebin_url_id
usock = urllib2.urlopen(pastebin_raw_url)
old_product_info = usock.read()
usock.close()
final_product_info = old_product_info + product_info
print 'Note: Exporting to pastebin only works if one is logged in on their default browser. Attempting now.'
pastebin_vars = {'api_dev_key':'57fe1369d02477a235057557cbeabaa1',
'api_option':'paste',
'api_paste_code':final_product_info}
response = urllib.urlopen('http://pastebin.com/api/api_post.php', urllib.urlencode(pastebin_vars))
pastebin_url = response.read()
print pastebin_url +'\n\n'
webbrowser.open_new_tab(pastebin_url)
else:
print 'Your input was not understood. Use "1" or "2" only.'
#product_category = 0 <- coming soon
# VARIABLE NAMES + DESCRIPTIONS
# newegg_URL - the url of the newegg product found in url
# soup - the beautiful soup html of the newegg product
# product_code - code of the product
# product_info_URL - url of the page that holds price and rating
# usock - urllib's product info url thing
# data - json interpretation of the page with details on it
# price - product price
# name_with_site - product name
# name - product name, but without the annoying "newegg.com - " part
# product_category - product category COMING SOON SO HARD TO DO D:
# todo: make it take more than one in one instance and make it export to SOMETHING
# (notepad? pastebin? SOMETHING?)