pycharm 在Python中使用Selenium进行网页抓取

iqih9akk  于 2022-11-08  发布在  PyCharm
关注(0)|答案(1)|浏览(226)

我正在尝试使用Python + Selenium + PhantomJS从Bloomberg上的汇率获取最新价格
这里是URL
下面是HTML

<div class="overviewRow__0956421f">
    <span class="priceText__1853e8a5">3.9100</span>
    <span class="currency__defc7184">BRL</span>
</div>

这是我的代码

from selenium import webdriver
my_url = 'https://www.bloomberg.com/quote/USDBRL:CUR'
driver = webdriver.PhantomJS()
driver.get(my_url)
price = driver.find_element_by_class_name("priceText__1853e8a5")
print(price)

不过是不刮。
以下是错误堆栈跟踪:

/Users/marcelo/PycharmProjects/extractwiki/venv/bin/python /Users/marcelo/PycharmProjects/extractwiki/wiki.py
/Users/marcelo/PycharmProjects/extractwiki/venv/lib/python3.7/site-packages/selenium/webdriver/phantomjs/webdriver.py:49: UserWarning: Selenium support for PhantomJS has been deprecated, please use headless versions of Chrome or Firefox instead
  warnings.warn('Selenium support for PhantomJS has been deprecated, please use headless '
Traceback (most recent call last):
  File "/Users/marcelo/PycharmProjects/extractwiki/wiki.py", line 8, in <module>
    price = driver.find_element_by_class_name("overviewRow__0956421f")
  File "/Users/marcelo/PycharmProjects/extractwiki/venv/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 563, in find_element_by_class_name
    return self.find_element(by=By.CLASS_NAME, value=name)
  File "/Users/marcelo/PycharmProjects/extractwiki/venv/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 966, in find_element
    'value': value})['value']
  File "/Users/marcelo/PycharmProjects/extractwiki/venv/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 320, in execute
    self.error_handler.check_response(response)
  File "/Users/marcelo/PycharmProjects/extractwiki/venv/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: {"errorMessage":"Unable to find element with class name 'overviewRow__0956421f'","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Content-Length":"110","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:54302","User-Agent":"selenium/3.14.0 (python mac)"},"httpVersion":"1.1","method":"POST","post":"{\"using\": \"class name\", \"value\": \"overviewRow__0956421f\", \"sessionId\": \"1eaf82f0-a39a-11e8-867d-9dbde70c7bc5\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/1eaf82f0-a39a-11e8-867d-9dbde70c7bc5/element"}}
Screenshot: available via screen

Process finished with exit code 1

能请Maven帮我一下吗?

zf9nrax1

zf9nrax11#

请尝试以下操作:
从Selenium导入Web驱动程序
我的网址是:
驱动程序=网页驱动程序.PhantomJS()
驱动程序.get(my_url)
价格=驱动程序.按类别名称查找元素(“priceText__1853e8a5”)
对于价格中的值:打印(值.文本)

相关问题