【发布时间】:2017-02-12 10:54:03
【问题描述】:
我想检索网页的所有可见内容。比如说this 网页。我正在远程使用带有 selenium 的无头 Firefox 浏览器。
我使用的脚本是这样的
driver = webdriver.Remote('http://0.0.0.0:xxxx/wd/hub', desired_capabilities)
driver.get(url)
dom = BeautifulSoup(driver.page_source, parser)
f = dom.find('iframe', id='dsq-app1')
driver.switch_to_frame('dsq-app1')
s = driver.page_source
f.replace_with(BeautifulSoup(s, 'html.parser'))
with open('out.html', 'w') as fe:
fe.write(dom.encode('utf-8'))
这应该加载页面,解析 dom,然后用它的可见内容替换 id dsq-app1 的 iframe。如果我通过我的 python 命令行一一执行这些命令,它会按预期工作。然后我可以看到所有可见内容的段落。相反,当我通过执行脚本或将所有这些 sn-p 粘贴到我的解释器中一次执行所有这些命令时,它的行为会有所不同。段落不见了,内容还是json格式的,但不是我想要的。
知道为什么会发生这种情况吗?可能与replace_with 有关?
【问题讨论】:
标签: python html selenium beautifulsoup