python - Python selenium .get_attribute('innerHTML') 返回的值与页面源中的值不同
问题描述
我正在解析一个包含 Selenium 的 URL 的表。
某些 URL 的处理方式不正确。例如:
- URL 如何在源代码和页面中显示:
http://domain.tld/forum/viewtopic.php?f=4&t=25&view=next
- 浏览器检测工具使用
.get_attribute('innerHTML')
或使用后URL的显示方式: .Copy element
http://domain.tld/forum/viewtopic.php?f=4&t=25&view=next
如您所见,在这种情况下,它确实amp;
在每个&
. 如何使输出与第一种情况完全相同?我尝试使用innerHTML
和text
作为属性。text
返回无。
从表中获取 URL 的代码如下:
rows = driver.find_element_by_tag_name('tbody').find_elements_by_tag_name('tr')
for row in rows:
fields = row.find_elements_by_tag_name('td')
url = fields[0].get_attribute('innerHTML')
我在这里使用 Firefox 作为浏览器。
解决方案
UPD:text
没有从不在可见区域中的行返回值(不确定这个)o_O
最初我使用html2text
图书馆。
以下代码按预期返回数据:
from html2text import HTML2Text as H2T
rows = driver.find_element_by_tag_name('tbody').find_elements_by_tag_name('tr')
for row in rows:
fields = row.find_elements_by_tag_name('td')
url = H2T().handle(fields[0].get_attribute('innerHTML')).strip()
推荐阅读
- python - 如何在一维数组上使用 SimpleImputer?
- javascript - myMap 有没有办法进行身份验证
- python - Python网络抓取嵌套字典键对 - AttributeError
- django - 如何从基于 Django 的站点管理任务的停止或重新启动?
- error-handling - 在游戏中;内生关系操作需要模型类型“dnlp”错误
- oracle - 无法读取 oracle 中的特殊符号
- javascript - 如何解决 React Hook 关闭问题?
- javascript - 如何在Javascript中对字符串进行真实身份相等性检查
- promise - Elixir 对 Promise 或异步等待的回调
- javascript - 使用 createTemplateFromFile 和 getContent() 格式化表 html 上的 GmailApp htmlBody 错误