python - Trying to obtain the title of a website that is generated through javascript
问题描述
When I load up a website, its title is "Loading..." for a second, until it changes to a different title obtained through javascript. In python, I'm trying to obtain the generated title.
I've tried using selenium and PhantomJS, but I run into a number of problems.
from selenium import webdriver
driver = webdriver.PhantomJS(executable_path='/usr/local/lib/phantoms/bin/phantomjs')
driver.get(www.google.com)
p_element = driver.find_element_by_id(id='intro-text')
print(p_element.text)
That says that the support for PhantomJS has been deprecated, so I'm not sure what else to do, or if there's an easier method to obtain the result I need.
解决方案
使用 Firefox 或 Chrome 之类的东西,然后使用驱动程序的 title 属性
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://www.google.com")
print(driver.title)
如果标题短暂显示“正在加载...”,那么您可以循环获取标题,直到它不再显示“正在加载...”
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://www.google.com")
while driver.title == 'Loading...': //Should loop here until title changes
pass
print(driver.title)
我的蟒蛇生锈了,但这应该很接近。
推荐阅读
- cmake - libwebsockets(在 ubuntu 上) - 尝试编译示例“lws 最小 ws 服务器 + permessage-deflate echo” - 找不到 libwebsocketsConfig.cmake
- reactjs - 第二次单击编辑按钮时缺少 ID
- c# - 尝试从 Azure CI/CD 获取生成输出工件
- c++ - 当没有调整大小/重新散列发生时,C++ 普通的旧数组/向量/unordered_map 线程是否安全?
- reactjs - 如何获取所有 Reactjs 组件将使用的数据
- amazon-web-services - 无法从 SQL Developer 连接 AWS RDS
- docker - Dockerfile MKDIR 不工作(节点:高山)
- amazon-web-services - AWS Cloudfront 将 index.html 中的 CSS 和 JS 引用重写为 index_files/style.css 而不是 css/style.css
- express - 如果我在标头中发送会话 ID,是否可以在没有 CSRF 令牌的情况下阻止 CSRF 攻击
- python-3.x - 取消 asyncio 任务导致“任务被破坏但它处于挂起状态”