使用的库
使用到了selenium库和BeautifulSoup这两个库.要想使用这个程序就要首先在安装这两个库.对于selenium来说,还要安装webDriver,这个分别按照自己的浏览器来安装.我使用的是Chrome.
为什么要用selenium呢?
因为牛客网的面经展示页面时根据滚动条的移动利用ajax动态加载的,没有翻页,最方便的方式就是利用selenium访问网站,等到资源加载完成之后再进行爬取.
具体代码
import time
import docx
from bs4 import BeautifulSoup
import requests
from selenium import webdriver
urlbase = "https://www.nowcoder.com"
doc = docx.Document()
def getIndexPage(url):
driver = webdriver.Chrome()
#这个是牛客网java实习面经的界面,可以根据自己的需要,找好网页,然后将连接复制到这里来
driver.get("https://www.nowcoder.com/discuss/experience?tagId=639&order=3&companyId=0&phaseId=1")
time.sleep(3)
js = "return action=document.body.scrollHeight"
height = driver.execute_script(js)
driver.execute_script('window.scrollTo(0, document.body.scrollHeight)')
time.sleep(5)
t1 = int(time.time())
status