By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,790 Members | 1,258 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,790 IT Pros & Developers. It's quick & easy.

Getting a specific value from a website using python, takes an argument

P: 6
Expand|Select|Wrap|Line Numbers
  1. def money(coinPrice):
  2.   import urllib
  3.   connection = urllib.urlopen("")
  4.   rate =
  5.   connection.close()
  6.   currentLoc = rate.find("> coinPrice </A></div>")
  7.   if currentLoc != -1:
  8.     rateLocEnd = rate.rfind('</TD>', 0, currentLoc)
  9.     rateLocStart = rate.rfind('<TD width = "50">', 0, rateLocEnd)
  10.     rateCloseGold = rate[rateLocStart+4:rateLocEnd]
  11.     print "The price of", coinPrice, "Gold in US$ is", rateCloseGold

here is what I have, and the goal is to print out a statement that shows the closing price of gold in us dollars, by inputing what kind of gold you would like as an argument. I am not sure why this is ont working for me, it prints out much more than the 1 line in my print statement and I have copied a program that works properlly, but this one does not, I am guessing it has something to do with the fact that this one takes an argument. please help.
Nov 27 '15 #1

✓ answered by

Share this Question
Share on Google+
3 Replies

P: 16
Can I see the output please??
Nov 28 '15 #2

P: 6
yes here it is, it prints out a very large portion of the website source, but i only need it to print the cost in US$ of whatever argument is passed through it.
I would like to note the output is about 5x longer than this but only posted the very beginning and end because it would not let me post the reply otherwise.
======= Loading Program =======
Expand|Select|Wrap|Line Numbers
  1. >>> howMuch("Austrian")
  2. The price of Austrian Gold in US$ is TYPE html>
  3. <HTML>  
  4. <HEAD>
  7.   <META NAME="GENERATOR" CONTENT="Adobe PageMill 3.0 Mac">
  8.   <TITLE>Todays Gold Coin Prices - closing price of gold bars and coins</TITLE>
  9.   <META NAME="description" CONTENT="End of day gold price quotes for coins and bullion, plus live gold coin prices, up to the minute">
  10.   <META NAME="keywords" CONTENT="gold coin prices, gold price, price of gold, price of gold coins, coin prices, coins">
  11. <style type="text/css">
  12. .txt9 {
  13.     font-family: Verdana, Geneva, sans-serif;
  14.     font-size: 9pt;
  15. }
  16. .txt8Center {
  17.     font-family: Verdana, Geneva, sans-serif;
  18.     font-size: 8pt;
  19.     text-align: center;
  20. }
  21. November 25, 2015 </TD>
  22.             <TD WIDTH="33%" BGCOLOR="#FFFFFF" NOWRAP><P align="center"> Spot gold price:<BR>
  23.                $ 1,071.06</TD>
  24.             <TD WIDTH="34%" BGCOLOR="#eeeeee"><P align="center"> Spot silver price:<BR>
  25.                $ 14.18 
  26. >>>
Nov 28 '15 #3

P: 16
Either your scraping logic is wrong or the website changed its source code after you used your logic. I would suggest you to use. BeautifulSoup to extract the data.
BS is a third party library but famous due to is helpfulness.

Use BeautifulSoup to extract the table that has the required values.And then extract the required table row depending on the type of gold chosen by the user.
Nov 29 '15 #4

Post your reply

Sign in to post your reply or Sign up for a free account.