您的位置:首页技术文章
文章详情页

Python爬虫实现模拟点击动态页面

浏览:21日期:2022-08-03 18:17:33

动态页面的模拟点击:

以斗鱼直播为例:http://www.douyu.com/directory/all

爬取每页的房间名、直播类型、主播名称、在线人数等数据,然后模拟点击下一页,继续爬取

代码如下

#!/usr/bin/python3# -*- coding:utf-8 -*-__author__ = ’mayi’ '''动态页面的模拟点击: 模拟点击斗鱼直播:http://www.douyu.com/directory/all 爬取每页房间名、直播类型、主播名称、在线人数等数据,然后模拟点击下一页,继续爬取''' from selenium import webdriverimport json # 调用环境变量指定的PhantomJS浏览器创建浏览器对象,executable_path:指定PhantomJS位置driver = webdriver.PhantomJS(executable_path = r'D:Program Filesphantomjsbinphantomjs')from bs4 import BeautifulSoup class DouyuSpider(object): ''' 爬虫类 ''' def __init__(self): self.url = 'http://www.douyu.com/directory/all/' self.driver = webdriver.PhantomJS() self.file_name = open('douyu.json', 'w', encoding = 'utf-8') def run(self): ''' 爬虫开始工作 ''' self.driver.get(self.url) # 循环处理每一页,直至最后一页 page = 1 start_flag = True while True: # 等待3秒,防止访问过于频繁 self.driver.implicitly_wait(3) print('正在处理第' + page + '页......') page += 1 # 解析 soup = BeautifulSoup(self.driver.page_source, 'lxml') # 在线直播部分 online_live = soup.find_all(’ul’, {’id’: ’live-list-contentbox’})[0] # 房间列表 live_list = online_live.find_all(’li’) # 处理每一个房间 for live in live_list:# 房间名、直播类型、主播名称、在线人数# 房间名home_name = live.find_all(’h3’, {’class’: ’ellipsis’})[0].get_text().strip()# 直播类型live_type = live.find_all(’span’, {’class’: ’tag ellipsis’})[0].get_text().strip()# 主播名称anchor_name = live.find_all(’span’, {’class’: ’dy-name ellipsis fl’})[0].get_text().strip()# 在线人数online_num = live.find_all(’span’, {’class’ :’dy-num fr’})[0].get_text().strip()# print(home_name, live_type, anchor_name, online_num)item = {}item['房间名'] = home_nameitem['直播类型'] = live_typeitem['主播名称'] = anchor_nameitem['在线人数'] = online_numif start_flag: start_flag = False content = '[n' + json.dumps(item)else: content = ',n' + json.dumps(item)self.file_name.write(content) # page_source.find()未找到内容则返回-1 if self.driver.page_source.find(’shark-pager-disable-next’) != -1:# 已到最后一页break # 模拟点击下一页 self.driver.find_element_by_class_name(’shark-pager-next’).click() # 爬虫结束前关闭文件 self.file_name.write('n]') self.file_name.close()if __name__ == ’__main__’: douyu = DouyuSpider() douyu.run()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持好吧啦网。

标签: Python 编程
相关文章: