Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.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
Python中的BeautifulSoup-未显示DIV内容_Python_Beautifulsoup - Fatal编程技术网

Python中的BeautifulSoup-未显示DIV内容

Python中的BeautifulSoup-未显示DIV内容,python,beautifulsoup,Python,Beautifulsoup,我想首先说,我在这个网站上回顾了几个解决方案,但似乎没有一个对我有效 我只是想从这个网站上访问div标签的内容,但是内容没有显示出来 以下是我目前掌握的代码: SpotifyGlobViralurl='https://play.spotify.com/chart/3S3GshZPn5WzysgDvfTywr' browser.get(SpotifyGlobViralurl) page = browser.page_source soup = BeautifulSoup(page) #the di

我想首先说,我在这个网站上回顾了几个解决方案,但似乎没有一个对我有效

我只是想从这个网站上访问div标签的内容,但是内容没有显示出来

以下是我目前掌握的代码:

SpotifyGlobViralurl='https://play.spotify.com/chart/3S3GshZPn5WzysgDvfTywr'
browser.get(SpotifyGlobViralurl)
page = browser.page_source
soup = BeautifulSoup(page)
#the div contents exist in an iframe, so now we call the iframe contents of the 3rd iframe on page:
iFrames=[] 
iframexx = soup.find_all('iframe')
response = urllib2.urlopen(iframexx[3].attrs['src'])
iframe_soup = BeautifulSoup(response)
divcontents = iframe_soup.find('div', id='main-container')
我试图提取'main container'div的内容,但正如您将看到的,当存储在创建的divcontent变量中时,它显示为空。但是,如果您访问实际的URL并检查元素,您会发现这个“maincontainer”div语句中填充了它的所有内容


我非常感谢您的帮助。

这是因为容器是动态加载的。我注意到您正在使用
selenium
,您必须继续使用它,切换到iframe并等待
主容器加载:


这太好了,谢谢你,亚历克斯。作为一个后续问题,既然容器变量中存储了webdriver元素,那么我实际上如何刮取容器的内容呢?@user3882316这取决于您需要从容器中获取什么。如果只是文本,则使用
container.text
。也可以在其中找到其他元素,请参见。此外,要结束主题,请查看答案是否可以接受。谢谢
wait = WebDriverWait(browser, 10)

# wait for iframe to become visible
iframe = wait.until(EC.visibility_of_element_located((By.XPATH, "//iframe[starts-with(@id, 'browse-app-spotify:app:chart:')]")))
browser.switch_to.frame(iframe)

# wait for header in the container to appear
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#main-container #header")))

container = browser.find_element_by_id("main-container")