这是否可以在使用 python 执行 javascript 后加载页面? [英] Is this possible to load the page after the javascript execute using python?

查看:21
本文介绍了这是否可以在使用 python 执行 javascript 后加载页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我阅读的页面:

<html>
<head>
<script type="text/javascript">
    document.write("Hello World")
</script>
</head>
<body>
</body>
</html>

如你所见,Hello World是使用javascript在HTML页面上添加的,当我使用HTML解析器时,如BeautifulSoup解析它,它可以不解析Hello World,我可以解析客户端真正看到的实际结果......?谢谢.

As you can see, the Hello World is added on the HTML page using javascript, when I use the HTML parser, like the BeautifulSoup to parse it, it can't parse the Hello World, it is possible to me parse the actually result on how the client side really see....? Thanks.

推荐答案

我在用 python 编写网络爬虫时遇到了类似的问题,我找到了 Selenium Web Driver 与 BeautifulSoup 结合使用非常有用.代码最终看起来像这样:

I ran into a similar problem when writing web scrapers in python, and I found Selenium Web Driver in combination with BeautifulSoup very useful. The code ends up looking something like this:

from selenium import webdriver
browser = webdriver.Firefox()
browser.get("http://www.yoursite.com")
soup = BeautifulSoup(browser.page_source, "html.parser")
...

使用 Selenium WebDriver,在功能上还有等待某个 DOM 元素加载"的功能,这也使 JavaScript 元素的计时更容易.

With Selenium WebDriver, there's also functionally for a "wait until a certain DOM element has loaded", which makes the timing with javascript elements easier too.

这篇关于这是否可以在使用 python 执行 javascript 后加载页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆