python爬取动态网页的内容

from    bs4     import BeautifulSoup
from selenium import webdriver
import time


def     main():
        driver = webdriver.Firefox()#用FireFox来渲染,chrome也可以,不过还要下载相应的exe文件
        driver.get('http://book.km.com/chapter/1462907_3.html')
        //小说的内容是用ajax取得的,普通的静态下载不下来
        time.sleep(4)
        srcCode = driver.page_source#获取渲染后的源码
        soup = BeautifulSoup(srcCode,"lxml")
        article = soup.find('div',{"class":"article-body"})#查找文章的位置
        if article!=None:

            print(article.text)
if      __name__=="__main__":
        main()

这里写图片描述
这里的一篇好文章:selenium用法

猜你喜欢

转载自blog.csdn.net/cosmopolitanme/article/details/80548939