[英]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:试图获取 MBBM 的当前股价,但它没有使用复制选择器(在 Chrome 上)提取它,如代码的 soup.select 部分所示:
import bs4, requests导入 bs4,请求
stockCode = MBBM URL = 'https://www.bursamarketplace.com/mkt/themarket/stock/' + stockCode stockCode = MBBM URL = 'https://www.bursamarketplace.com/mkt/themarket/stock/' + stockCode
def getStockPrice(URL): res = requests.get(URL) res.raise_for_status 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)价格 = getStockPrice(URL) 打印(价格)
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输出为无
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'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.