sarizal
sarizal

Reputation: 49

CSS Selector does not extract the data I want

Trying to get the current stock price for MBBM, but it doesnt extract it using copy selector (on Chrome)as seen in the soup.select part of code:

import bs4, requests

stockCode = MBBM URL = 'https://www.bursamarketplace.com/mkt/themarket/stock/' + stockCode

def getStockPrice(URL): res = requests.get(URL) res.raise_for_status

soup = bs4.BeautifulSoup(res.text, 'html.parser')
elems = soup.select('body > main > div > div > div > section > div.topPnl_cnt.row > div.movemBox.small-12.medium-12.large-2.column > div:nth-child(1) > div.priceBox.small-6.medium-6.large-12.column.downBox > div.value')
return elems[0].text.strip()

price = getStockPrice(URL) print(price)

import bs4, requests

#stockCode = input('Insert BursaMKTPLC stock code: \n')

stockCode = 'MBBM'
URL = 'https://www.bursamarketplace.com/mkt/themarket/stock/' + stockCode

res = requests.get(URL)
print(res.raise_for_status)

soup = bs4.BeautifulSoup(res.text, 'html.parser')

price = soup.find("div", {"name": "tixStockLast"}).text.strip()
print(price)

output is None

Upvotes: 0

Views: 131

Answers (1)

HedgeHog
HedgeHog

Reputation: 25196

Try to use Selenium

from selenium import webdriver
driver = webdriver.Chrome(executable_path=r'C:\Program Files\Chrome Driver\chromedriver.exe') #replace with your path to chromedriver

stockCode = 'MBBM'
URL = 'https://www.bursamarketplace.com/mkt/themarket/stock/' + stockCode

driver.get(URL)

Give the page some time to load also dynamically generated information

driver.implicitly_wait(10) # wait for seconds

Find yourelement

elements = driver.find_element_by_name('tixStockLast')
elements.text

Output

'8.100'

Upvotes: 0

Related Questions