0

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

sarizal
  • 49
  • 1
  • 5
  • Please restructure your question and provide some more information - Concrete url, element, output would help to understand – HedgeHog Nov 23 '20 at 08:07
  • Thanks for that update looks much better - Output is non, cause `price element` information is generated dynamically and it is not present in the moment `request.get()` grap the sourcecode: `
    ` You should try [Selenium](https://selenium-python.readthedocs.io/getting-started.html) instead of `request` and use its waits to detect if `price` is loaded.
    – HedgeHog Nov 23 '20 at 10:17

1 Answers1

0

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'
Dharman
  • 30,962
  • 25
  • 85
  • 135
HedgeHog
  • 22,146
  • 4
  • 14
  • 36
  • I attempted this and got this error: DevTools listening on ws://127.0.0.1:61031/devtools/browser/428ea207-2de1-4bec-967c-5da458a73284 [6228:16304:1126/150954.088:ERROR:device_event_log_impl.cc(211)] [15:09:54.087] Bluetooth: bluetooth_adapter_winrt.cc:1073 Getting Default Adapter failed. Tried: updated Chrome, chromedriver and selenium – sarizal Nov 26 '20 at 07:11
  • Note sure why this error should occur, this answer may help: https://stackoverflow.com/questions/61561112/how-to-solve-getting-default-adapter-failed-error-when-launching-chrome-and-tr?answertab=votes#tab-top – HedgeHog Nov 26 '20 at 10:49
  • I used Firefox as my webdriver instead and it works fine, thank you for help!! – sarizal Nov 28 '20 at 12:45