Search code examples
pythonweb-scrapingbeautifulsoup

trying to test a code that scrape from yahoo finance


I'm a python beginner but I like to learn the language by testing it and trying it.

so there is a yahoo web scraper code to scrape the last price of a specific stock, but it doesn't work with me I don't know the problem.

..

The code :

# -*- coding: utf-8 -*-
import bs4
import requests
from bs4 import BeautifulSoup


def parsePrice():
    r = requests.get('https://finance.yahoo.com/quote/fb?p=FB')
    soup = bs4.BeautifulSoup(r.text,"xml")
    price = soup.find('div', {'class':'My(6px) Pos(r) smartphone_Mt(6px)'}).find('span').text
    return price

while True:
    print('the current price: '+str(parsePrice()))

error :

price = soup.find('div', {'class':'My(6px) Pos(r) smartphone_Mt(6px)'}).find('span').text
AttributeError: 'NoneType' object has no attribute 'find'

maybe it's an easy thing but remember I'm a beginner.

Thanks in advance


Solution

  • Use lxml as a parser instead of xml.

    There's no need to convert parsePrice to a string when printing, since it's output is already a string.

    import requests
    from bs4 import BeautifulSoup
    
    def parsePrice():
        r = requests.get('https://finance.yahoo.com/quote/fb?p=FB')
        soup = BeautifulSoup(r.text,"lxml")
        price = soup.find('div', {'class':'My(6px) Pos(r) smartphone_Mt(6px)'}).find('span').text
        return price
    
    print('the current price: ' + parsePrice())
    

    Outputs:

    the current price: 216.08