Web scraping Python:需要等待BeautifulSoup和Urllib才能解析网站

Web scraping Python:需要等待BeautifulSoup和Urllib才能解析网站,web-scraping,beautifulsoup,urllib,Web Scraping,Beautifulsoup,Urllib,我试图实时获取当前世界人口,但当网页首次加载时,检索数据需要几秒钟。当我运行程序时,我得到加载。。。而不是人口数量。有没有办法等到网页完全加载后再检索信息? 提前谢谢 代码如下: import urllib.request from bs4 import * htmlfile = urllib.request.urlopen("http://www.theworldcounts.com/counters/shocking_environmental_facts_and_statistics/w

我试图实时获取当前世界人口,但当网页首次加载时,检索数据需要几秒钟。当我运行程序时,我得到加载。。。而不是人口数量。有没有办法等到网页完全加载后再检索信息? 提前谢谢

代码如下:

import urllib.request
from bs4 import *

htmlfile = urllib.request.urlopen("http://www.theworldcounts.com/counters/shocking_environmental_facts_and_statistics/world_population_clock_live")

htmltext = htmlfile.read()

soup = BeautifulSoup(htmltext)
body = soup.find(text="World population").find_previous('p')

print (body.text)

您需要一个引擎,它可以从您正在下载的网页中解释javascript

更好的解决方案是找到该站点的静态版本或其他包含此类信息的站点(我确信该站点实际上不提供任何信息-仅提供外推数据)

但是如果你真的想使用dryscape,你可以使用这种方法

   # visiting desired site
   session.set_html("<html></html>")
   session.visit(link)
   # wait 
   session.driver.wait_for(lambda: watToWait(session))

嗯,您可以添加time.sleep(30),等待30秒,或者在循环过程中直到值未加载…您以前运行过此程序吗?它似乎对我不起作用:(实际上我没有,我猜你需要一个javascript解析器。你不认为用python可以做到这一点吗?是的,这是可能的,它只是你需要的模块。它不漂亮。你需要一些javascript刮刀。也许这个:OP,你为什么不共享你找到的链接?
   # visiting desired site
   session.set_html("<html></html>")
   session.visit(link)
   # wait 
   session.driver.wait_for(lambda: watToWait(session))
   def watToWait(session):
        soup = BeautifulSoup(session.body(), BEAUTIFUL_SOUP_PARSER)
        column = soup.find('td')
        if column is not None:
            return True
        else:
            return False