如何在 python BeautifulSoup 或任何其他模块中获得 javascript 输出
How to get javascript output in python BeautifulSoup or any other module
在尝试制作抓取工具时,我发现一个网站在其代码中大量使用 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>
但是我需要获取 "Your Number is: 8437480",我可以使用 soup.span.get_text() 获取 span 之间的文本,但是我无法获取脚本的编号。
Beautifulsoup就是不能执行javascript代码。我建议您将 PhantomJS 之类的东西集成到您的 scraper 中。如果你可以删除 python,你 scraper in PhantomJS
在尝试制作抓取工具时,我发现一个网站在其代码中大量使用 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>
但是我需要获取 "Your Number is: 8437480",我可以使用 soup.span.get_text() 获取 span 之间的文本,但是我无法获取脚本的编号。
Beautifulsoup就是不能执行javascript代码。我建议您将 PhantomJS 之类的东西集成到您的 scraper 中。如果你可以删除 python,你 scraper in PhantomJS