Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
BeautifulSoup等待JavaScript/Angular内容_Javascript_Python_Html_Angularjs_Beautifulsoup - Fatal编程技术网

BeautifulSoup等待JavaScript/Angular内容

BeautifulSoup等待JavaScript/Angular内容,javascript,python,html,angularjs,beautifulsoup,Javascript,Python,Html,Angularjs,Beautifulsoup,我正在尝试使用python从某个url获取所有图像 因此,beautiful soup的使用是向前发展的,但我面临的问题是,并非所有img标签都打印在控制台中。仔细查看所需的HTML文件,会发现缺少的图像来自Angular,因为它们有一个data ng src标记 有没有办法告诉soup等到所有脚本都完成了?或者是否有其他方法检测所有img标签 到目前为止,我的代码是: import urllib2 from BeautifulSoup import BeautifulSoup page =

我正在尝试使用python从某个url获取所有图像

因此,beautiful soup的使用是向前发展的,但我面临的问题是,并非所有img标签都打印在控制台中。仔细查看所需的HTML文件,会发现缺少的图像来自Angular,因为它们有一个data ng src标记

有没有办法告诉soup等到所有脚本都完成了?或者是否有其他方法检测所有img标签

到目前为止,我的代码是:

import urllib2
from BeautifulSoup import BeautifulSoup

page = BeautifulSoup(urllib2.urlopen(url))
allImgs = imgs = page.findAll('img')
print allImgs

图像不会插入HTML页面,而是链接到该页面。 对于需要等待/暂停时间的事情,我宁愿 使用SeleniumWeb驱动程序。我认为《美丽的汤》是一本书 一下子。我认为这是一个令人畏惧的包装
解析文件的琐事,但不是作为与页面交互的工具。

您可以尝试使用selenium。尽管此库用于自动化测试,但它的功能比BeautifulSoup丰富得多

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

url ='http://example.com/'
driver = webdriver.Firefox()
driver.get(url)

delay = 5 # seconds

try:
    WebDriverWait(driver, delay).until(EC.presence_of_element_located(driver.find_elements_by_xpath('..//elementid')))
    print "Page is ready!"
    for image in driver.find_elements_by_xpath('..//img[@src]'):
        print image.get_attribute('src')
except TimeoutException:
    print "Couldn't load page"
也请阅读下面的帖子;谈论使用JS动态加载页面

可能存在的副本