我希望能够列出已发表评论的用户,但在使用 BeautifulSoup 拉取时无法在页面上找到 iframe。注释在 iframe 中,出于某种原因,当我使用 BeautifulSoup 拉取 html 时,其中似乎没有 iframe。我知道有一个iframe可以保存评论,因为我查看了网页上的html,以便尝试向下钻取并使用BeautifulSoup提取我需要的内容。
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,而不必像使用硒那样打开浏览器。这可能吗?如果没有,除了美丽汤之外,我还能用什么来做这件事?
iframe由JavaScript附加,JavaScript在页面加载后在合适的环境 - 浏览器中执行。BeautifulSoup不会以任何方式执行JS——它只是从确定的URL获取字符串并将其解析为HTML。