0

I am trying to scrape the price of the coin from the link below using the following Python Script, however I am running into some issues. If you could identify where I am going wrong then that would be great!

URL's for various coins from various websites are stored in a Python Dictionary called coins. A try, except, if stack is used to find the appropiate method to get the price. For some reason I am unable to exract the price from the following URL:

https://www.hattongardenmetals.com/buy/2020-gold-britannia-2

The script being used is:

    for coin in prices:
    response = requests.get(prices[coin]["url"])
    soup = BeautifulSoup(response.text, 'html.parser')

    try:
        text_price = soup.find(
            'td', {'id': 'price-inc-vat-per-unit-1'}).get_text()        
    except:
        text_price = soup.find(
            'td', {'id': 'total-price-inc-vat-1'}).get_text()            
    else:
        text_price = soup.find(
            'span', {'class': 'woocommerce-Price-amount amount'}).get_text       #<------

For some reason the line highlighted by the arrow keeps getting this error:

AttributeError: 'NoneType' object has no attribute 'get_text'

How can I fix this and how does you fix integrate with the code provided?

arthem
  • 141
  • 3
  • 13
  • 1
    Does this answer your question? [Beautifulsoup Python unable to scrape data from a website](https://stackoverflow.com/questions/61158222/beautifulsoup-python-unable-to-scrape-data-from-a-website) – David Fisher Jul 18 '20 at 17:30

2 Answers2

2

The price is inside <span> tag with class="h2 d-block":

import requests
from bs4 import BeautifulSoup


url = 'https://www.hattongardenmetals.com/buy/2020-gold-britannia-2'
soup = BeautifulSoup(requests.get(url).content, 'html.parser')

print( soup.select_one('span.h2').text )

Prints:

£1584.31
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
1

try putting a parenthesis .. after get_text text_price = soup.find('span', {'class': 'woocommerce-Price-amount amount'}).get_text()

fardV
  • 198
  • 9