Python 使用Selenium获取债券最后收盘价时Xpath出错

Python 使用Selenium获取债券最后收盘价时Xpath出错,python,selenium,beautifulsoup,selenium-chromedriver,Python,Selenium,Beautifulsoup,Selenium Chromedriver,我无法得出finra债券的最后交易价格。我尝试使用Beautiful Soup,但它找不到div标记,我目前正在尝试使用Selenium来获得相同的结果 我附上了我的代码截图,以及我在尝试执行代码时收到的错误消息 提前谢谢 我的代码: from selenium.webdriver.common.keys import Keys import pandas as pd driver = webdriver.Chrome(My Path Here) url = 'http://finra-mar

我无法得出finra债券的最后交易价格。我尝试使用Beautiful Soup,但它找不到
div
标记,我目前正在尝试使用Selenium来获得相同的结果

我附上了我的代码截图,以及我在尝试执行代码时收到的错误消息

提前谢谢

我的代码:

from selenium.webdriver.common.keys import Keys
import pandas as pd

driver = webdriver.Chrome(My Path Here)
url = 'http://finra-markets.morningstar.com/BondCenter/BondDetail.jsp?ticker=FSBIN4730902&symbol=SBIN4730902'
driver.get(url)
xpath = '//span[@id="price"'
Market_Price = driver.find_element_by_xpath(xpath)
driver.close()
print(Market_Price)```


Error message : `selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //span[@id="price" because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//span[@id="price"' is not a valid XPath expression.`



在这种情况下,错误消息非常具有描述性:

SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//span[@id="price"' is not a valid XPath expression.`
这意味着您的
xpath
表达式无效,因为它缺少结束符
]

应该是这样的:

xpath = '//span[@id="price"]'

在这种情况下,错误消息非常具有描述性:

SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//span[@id="price"' is not a valid XPath expression.`
这意味着您的
xpath
表达式无效,因为它缺少结束符
]

应该是这样的:

xpath = '//span[@id="price"]'

如果您查看网络选项卡,您可以看到动态返回该价格的回调。您可以发出该请求的简化版本,并从返回的字符串中输出所需的值。为了获取
srtqs
cookie,您需要向原始url发出带有会话的初始请求

import requests, re

params = (
    ('t', 'FSBIN4730902'),
    ('region', 'usa'),
    ('culture', 'en-US'),
    ('productcode', 'QS'),
    ('cur', ''),
    ('refresh', 'true'),
    ('callback', 'jQuery16407409943162048542_1609677517942'),
)

with requests.Session() as s:
    r = s.get('http://finra-markets.morningstar.com/BondCenter/BondDetail.jsp?ticker=FSBIN4730902&symbol=SBIN4730902')
    r = s.get('http://quotes.morningstar.com/bondq/quote/c-banner?&t=FSBIN4730902&region=usa&culture=en-US&productcode=QS&cur=&refresh=true&callback=jQuery16407409943162048542_1609677517942', params=params)
    print(re.search('"price.*\$([0-9.]+)', r.text).groups(0)[0])

正则表达式解释:


如果查看网络选项卡,您可以看到动态返回该价格的回调。您可以发出该请求的简化版本,并从返回的字符串中输出所需的值。为了获取
srtqs
cookie,您需要向原始url发出带有会话的初始请求

import requests, re

params = (
    ('t', 'FSBIN4730902'),
    ('region', 'usa'),
    ('culture', 'en-US'),
    ('productcode', 'QS'),
    ('cur', ''),
    ('refresh', 'true'),
    ('callback', 'jQuery16407409943162048542_1609677517942'),
)

with requests.Session() as s:
    r = s.get('http://finra-markets.morningstar.com/BondCenter/BondDetail.jsp?ticker=FSBIN4730902&symbol=SBIN4730902')
    r = s.get('http://quotes.morningstar.com/bondq/quote/c-banner?&t=FSBIN4730902&region=usa&culture=en-US&productcode=QS&cur=&refresh=true&callback=jQuery16407409943162048542_1609677517942', params=params)
    print(re.search('"price.*\$([0-9.]+)', r.text).groups(0)[0])

正则表达式解释:


您有几个问题:

  • 您的xpath表达式不正确;它缺少一个“*”
  • 您应该为
    隐式\u wait
    调用编写代码,以便为元素显示留出时间
  • 找不到元素的第三个原因是它位于必须首先切换到的
  • 您应该发出
    驱动程序。退出
    调用,而不是
    驱动程序。关闭
    调用,除了关闭窗口外,还可以终止驱动程序进程
  • 印刷品:

    <selenium.webdriver.remote.webelement.WebElement (session="357c5c44e981ff0adde57969c24fb638", element="a9c141e2-8f35-4ff0-9970-90d083dd0476")>
    

    您有几个问题:

  • 您的xpath表达式不正确;它缺少一个“*”
  • 您应该为
    隐式\u wait
    调用编写代码,以便为元素显示留出时间
  • 找不到元素的第三个原因是它位于必须首先切换到的
  • 您应该发出
    驱动程序。退出
    调用,而不是
    驱动程序。关闭
    调用,除了关闭窗口外,还可以终止驱动程序进程
  • 印刷品:

    <selenium.webdriver.remote.webelement.WebElement (session="357c5c44e981ff0adde57969c24fb638", element="a9c141e2-8f35-4ff0-9970-90d083dd0476")>
    
    
    
    请通读并相应更新您的问题。始终将您尝试过的代码和任何错误添加为文本,而不是屏幕截图。感谢您的反馈。我已经做了必要的修改。请通读并相应更新您的问题。始终将您尝试过的代码和任何错误添加为文本,而不是屏幕截图。感谢您的反馈。我已进行了必要的更改。您可能需要用户代理标头?你自己没试过吗?即使GET返回b“验证失败”,正则表达式也返回
    None
    ,对
    groups
    的调用也会引发异常。您似乎需要授权才能发出此请求。您可能需要用户代理标头?你自己没试过吗?即使GET返回b“验证失败”,正则表达式也返回
    None
    ,对
    groups
    的调用也会引发异常。您似乎需要授权才能提出此请求。