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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jackie123
    New Member
    • Nov 2015
    • 6

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

    Code:
    def money(coinPrice):
      import urllib
      connection = urllib.urlopen("http://www.usagold.com/gold/price.html")
      rate = connection.read()
      connection.close()
      currentLoc = rate.find("> coinPrice </A></div>")
      if currentLoc != -1:
        rateLocEnd = rate.rfind('</TD>', 0, currentLoc)
        rateLocStart = rate.rfind('<TD width = "50">', 0, rateLocEnd)
        rateCloseGold = rate[rateLocStart+4:rateLocEnd]
        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.
    Last edited by jackie123; Nov 27 '15, 09:31 PM. Reason: small error in code
  • jaseel97
    New Member
    • Feb 2015
    • 16

    #2
    Can I see the output please??

    Comment

    • jackie123
      New Member
      • Nov 2015
      • 6

      #3
      program output

      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 =======
      Code:
      >>> howMuch("Austrian")
      The price of Austrian Gold in US$ is TYPE html>
      <HTML>  
      <HEAD>
         
         
        <META NAME="GENERATOR" CONTENT="Adobe PageMill 3.0 Mac">
        <TITLE>Todays Gold Coin Prices - closing price of gold bars and coins</TITLE>
        <META NAME="description" CONTENT="End of day gold price quotes for coins and bullion, plus live gold coin prices, up to the minute">
        <META NAME="keywords" CONTENT="gold coin prices, gold price, price of gold, price of gold coins, coin prices, coins">
      <style type="text/css">
      .txt9 {
      	font-family: Verdana, Geneva, sans-serif;
      	font-size: 9pt;
      }
      .txt8Center {
      	font-family: Verdana, Geneva, sans-serif;
      	font-size: 8pt;
      	text-align: center;
      }
      November 25, 2015 </TD>
                  <TD WIDTH="33%" BGCOLOR="#FFFFFF" NOWRAP><P align="center"> Spot gold price:<BR>
                     $ 1,071.06</TD>
                  <TD WIDTH="34%" BGCOLOR="#eeeeee"><P align="center"> Spot silver price:<BR>
                     $ 14.18 
      >>>
      Last edited by Rabbit; Nov 28 '15, 04:44 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.

      Comment

      • jaseel97
        New Member
        • Feb 2015
        • 16

        #4
        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.
        Last edited by zmbd; Nov 29 '15, 03:56 PM. Reason: <same info, two posts>

        Comment

        Working...