Python + selenium 也获取不了这个网页的源码

2020-06-14 22:48:11 +08:00
 DonnyChao

最近有个网页整体翻译的需求,看了一下,Google 网页翻译是最优解,但是使用 selenium 也获取不了网页翻译后的源码。

网页对象链接( Google 网页翻译页面): https://translate.google.com.hk/translate?hl=&sl=en&tl=zh-CN&u=https://docs.python.org/3/tutorial/index.html

使用 selenium 取这个网页的源码,但是只能获取到 head 源码,body 主要的信息获取不到,请问有什么方法能获取到整个页面的源码吗?

[img]https://p.pstatp.com/origin/ff450002d304a982cd55[/img]

3492 次点击
所在节点    Python
11 条回复
DonnyChao
2020-06-14 22:48:42 +08:00
noqwerty
2020-06-14 22:51:57 +08:00
Google 的 Cloud Translation 可以直接调 API 的: https://cloud.google.com/translate/docs/
laoyur
2020-06-14 22:52:42 +08:00
处理 iframe 时,要先 driver.switch_to.frame(frames[0])一下才行
9yu
2020-06-14 22:52:53 +08:00
检查有没有 iframe
xiri
2020-06-14 22:59:33 +08:00
为什么不直接调用 API
jizhihaoSAMA
2020-06-14 23:04:01 +08:00
iframe 正解
DonnyChao
2020-06-14 23:06:02 +08:00
@laoyur
@9yu

是 iframe 原因,已按 3 楼方法解决。
DonnyChao
2020-06-14 23:07:06 +08:00
@xiri Google 翻译 API 看网上教程说,不能翻译 HTML 文件,只能翻译文本
sxd96
2020-06-15 01:59:16 +08:00
如果只是这个网页的话,你知不知道这有中文选项...
polarpy
2020-06-15 11:37:46 +08:00
正解
yucongo
2020-06-16 23:08:58 +08:00
用 Pyppeteer 吧,还能异步操作。有现成的基于 Pyppeteer 的包 https://pypi.org/project/deepl-tr-async/ pip install deepl-tr-async,可异步调 google 翻译或 deepl

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/681519

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX