案例需求:
1.使用selenium自动化爬虫爬取哔哩哔哩排行榜中舞蹈类的数据(包括视频标题、up主、播放量和评论量)
2.利用bs4进行数据解析和提取
3.将爬取的数据保存在本地json文件中
4.保存在excel文件中
分析:
1.请求url地址:https://www.bilibili.com/v/popular/rank/dance
2.加载等待事件,否则获取数据不充分
wait = WebDriverWait(self.browsers, 280) wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'rank-item'))) time.sleep(5)
3.获取相应内容
last_height = self.browsers.execute_script("return document.body.scrollHeight") while True: self.browsers.execute_script('window.scrollTo(0, document.body.scrollHeight);') time.sleep(5) data = self.browsers.page_source # 获取网页源码 self.parse_data(data=data) new_height = self.browsers.execute_script("return document.body.scrollHeight") if new_height == last_height: break last_height = new_height
4.使用bs4解析数据
soup = BeautifulSoup(data, 'lxml') titles