【发布时间】:2021-03-05 22:15:21
【问题描述】:
试图获取 MBBM 的当前股票价格,但它没有使用复制选择器(在 Chrome 上)提取它,如代码的 soup.select 部分所示:
导入 bs4,请求
股票代码 = MBBM URL = 'https://www.bursamarketplace.com/mkt/themarket/stock/' + 股票代码
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()
价格 = 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)
输出为无
【问题讨论】:
-
请重新整理您的问题并提供更多信息 - 具体的 url、元素、输出将有助于理解
-
感谢更新看起来好多了 - 输出不是,因为
price element信息是动态生成的,并且它现在不存在request.get()获取源代码:<div class="value" name="tixStockLast"><div class="loader loaderSmall"><div class="loader_hld"><img alt="" src="/img/loading.gif"/></div></div></div>你应该尝试@ 987654322@ 而不是request并使用它的等待来检测是否加载了price。
标签: python beautifulsoup