我正在尝试使用在JavaScript中生成的Python动态内容(此视频的观看次数和评论数量:http://v.youku.com/v_show/id_XMTM5NTI3NDY3Ng==.html?from=y1.3-idx-uhome-1519-20887.205805-205902.5-2)。
我熟悉BeautifulSoup(运行约0.1秒,但没有JavaScript评估)和Selenium(3秒以上,即使我停用CSS,图像等,但解释JS)。
有没有办法在没有实际使用浏览器的情况下在Python中抓取JavaScript函数的结果(假设这会减慢Selenium的速度)?
我的Selenium代码如下:
# coding=utf-8
import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
firefox_profile=webdriver.FirefoxProfile()
#firefox_profile.set_preference('permissions.default.stylesheet',2)
firefox_profile.set_preference('permissions.default.image',2)
firefox_profile.set_preference('dom.ipc.plugins.enabled.libflashplayer.so',2)
driver = webdriver.Firefox(firefox_profile=firefox_profile)
time.sleep(7)
start=time.time()
def getYoukuVideoStats(youkuVideoUrl):
driver.get(youkuVideoUrl)
text= driver.find_element_by_id("allnum_cmt").text
print text
youku_video_urls_full=["http://v.youku.com/v_show/id_XOTU2Nzc3NDYw.html"]
for youkuVideoUrl in youku_video_urls_full:
try:
getYoukuVideoStats(youkuVideoUrl)
print youkuVideoUrl
except Exception, e:
print "Error with video: "+youkuVideoUrl
print str(e)
print time.time()-start