如何在python BeautifulSoup或任何其他模块中获取javascript输出

时间:2015-06-18 13:46:11

标签: javascript python html web-scraping beautifulsoup

在我尝试制作刮刀时,我发现一个网站在其代码中使用了很多javascript,是否可以检索脚本的输出,例如。

<html>
<head>
<title>Python</title>
</head>
<body>
<script type="text/javascript" src='test.js'></script>
<p> some stuff <br>
more stuff <br>
code <br>
video <br>
picture <br>
movie <br>
. <br>
. <br>
. <br>
</p>
<span>Your Number is:  </span>
<script type="text/javascript">document.write(math(5, 10, 15));</script>
</body>
</html>

“test.js”有:

function math (a, b, c) {return a * b * c * c * a * b * c + a + b +c - a;}

当我使用BeautifulSoup时,它会显示代码本身,即:

<script type="text/javascript">document.write(math(5, 10, 15));</script>

但是我需要得到“你的号码是:8437480”,我可以通过使用soup.span.get_text()得到span之间的文本但是我不能得到脚本的数量。

1 个答案:

答案 0 :(得分:1)

Beautifulsoup无法执行javascript代码。我建议你将PhantomJS之类的内容整合到你的剪贴板中。如果你可以删除python,你可以在PhantomJS中编写整个剪贴簿