2016-03-03 33 views
3

我希望能够制作评论用户列表,但在使用BeautifulSoup进行拉取时无法在页面上找到iframe。评论是在iframe中,由于某些原因,当我用BeautifulSoup拉动html时,似乎并没有iframe。我知道有一个持有评论的iframe,因为我查看了网页上的html,以便尝试深入研究并使用BeautifulSoup提取我需要的内容。我可以用BeautifulSoup在javascript后面获得一个iframe吗?

from bs4 import BeautifulSoup 
from urllib import urlopen 

url = urlopen("http://www.datpiff.com/Curreny-Alchemist-Carrollton-Heist-mixtape.766213.html") 
bsObj = BeautifulSoup(url,"html.parser") 

frame_list = bsObj.findAll("iframe") 

for frame in frame_list: 
    print(frame) 

不过,我发现这个JavaScript可能的答案,我需要什么,但我想问问,我是想在为了某种方式运行此JavaScript的服务器保存此页相信我是一个用户,然后iframe出现?

<script language="javascript"> 
    var disqus_shortname = 'datpiff4'; 
    /* * * DON'T EDIT BELOW THIS LINE * * */ 
    (function() { 
     var s = document.createElement('script'); s.async = true; 
     s.type = 'text/javascript'; 
     s.src = '//' + disqus_shortname + '.disqus.com/count.js'; 
     (document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s); 
    }()); 
</script> 

我希望能够得到这个iframe中,而无需打开使用硒时就像一个浏览器。这可能吗?如果不是,除了BeautifulSoup以外,我还能用什么来做这件事?

回答

2

iframe通过JavaScript附加,该页面在页面加载到适当的环境 - 浏览器后执行。 BeautifulSoup不以任何方式执行JS - 它只是从确定的URL中获取字符串并将其解析为HTML。

+0

感谢您的回复!那么我怎样才能到达iframe呢?我不应该使用BeautifulSoup吗?如果是这样,我应该使用什么? – ImNotBot

+1

要获得iframe,您应该在执行JS后获取呈现的页面 - 这里是与答案相同的问题:http://stackoverflow.com/questions/7064109/how-to-parse-html-that-includes-javascript-代码http://stackoverflow.com/questions/11047348/is-this-possible-to-load-the-page-after-the-javascript-execute-using-python –

+0

谢谢,但我已经看到了这些网页。我可能不得不改变我的问题。我可以在不使用硒打开网络浏览器的情况下访问iframe吗?如果可能的话,我真的想要摆脱硒。 – ImNotBot

相关问题