javascript - 使用 Beautifulsoup 和 Selenium 从 JavaScript 驱动的页面解析 URL
问题描述
我想解析发生任何电子邮件的 Git 存储库中的所有 URL。我使用https://grep.app
编码:
from bs4 import BeautifulSoup
from selenium import webdriver
url = 'https://grep.app/search?current=100&q=%40gmail.com'
chrome = "/home/dev/chromedriver"
browser = webdriver.Chrome(executable_path=chrome)
browser.get(url)
html = browser.page_source
soup = BeautifulSoup(html, 'lxml')
tags = soup.select('a')
print(tags)
当代码启动时,Chrome 启动并加载结果页面,在 Chrome 的开发人员工具中,在源代码中我可以看到很多 URL 的 A 和 HREF。 页面来源
喜欢: lib/plugins/revert/lang/eu/lang.php
但我的代码只从页脚返回“标签”:
"[<a href="/"><span class="slashes">//</span>grep.app</a>, <a href="mailto:hello@grep.app">Contact</a>]"
据我了解JS解析有问题。请指教我做错了什么?
解决方案
代码:
from bs4 import BeautifulSoup
from selenium import webdriver
url = 'https://grep.app/search?current=100&q=%40gmail.com'
chrome = "/home/dev/chromedriver"
browser = webdriver.Chrome(executable_path=chrome)
browser.get(url)
html = browser.page_source
soup = BeautifulSoup(html, 'lxml')
links = []
tags = soup.find_all('a', href=True)
for tag in tags:
links.append(tag['href'])
print(links)
输出:
['/', 'mailto:hello@grep.app']
推荐阅读
- android - 何时在 Android 中使用无头活动和服务?
- angular - HTML
带有脚本示例的标记无法在 TypeScript / Angular 应用程序中编译
- python-3.x - 如何使用 python 在 Windows 系统上编辑 docx 和/或 doc 文件标签?
- c++ - 从双向链表末尾删除节点时出现分段错误
- php - 按字段对对象数组进行排序没有区别
- firebase - 如何创建包含地图的复合索引?
- r - R Shiny:使用复选框更新数据表
- r - 预测 R 函数没有返回置信区间,为什么?
- javascript - 制作一个简单的全局私有变量 - 访问正文元素
- python-3.x - 如何在我用 PyQt5 制作的 GUI 中显示来自 psql 数据库的数据?