Python使用Selenium模块模拟浏览器抓取斗鱼直播间信息示例

时间:2021-05-22

本文实例讲述了Python使用Selenium模块模拟浏览器抓取斗鱼直播间信息。分享给大家供大家参考,具体如下:

import timefrom multiprocessing import Poolfrom selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.common.exceptions import TimeoutExceptionfrom bs4 import BeautifulSoupfrom pymongo import MongoClientfrom pymongo.errors import PyMongoError# monogdb配置信息MONGO_HOST = "localhost"MONGO_DATABASE = "douyu"MONGO_TABLE = "zhibo"client = MongoClient(host=MONGO_HOST)db = client[MONGO_DATABASE]# PhantomJS 命令行相关配置# 参见 http://phantomjs.org/api/command-line.htmlSERVICE_ARGS = ['--disk-cache=true', '--load-images=false']# driver = webdriver.Chrome() # 有界面driver = webdriver.PhantomJS(service_args=SERVICE_ARGS) # 无界面delay = 10wait = WebDriverWait(driver, delay)driver.maximize_window()def get_total_pages(): url = 'https://pound class names not permitted room_list = get_rooms_by_beautifulsoup() save_to_monogodb(room_list) except TimeoutException: print("请求第%d页失败" % page_num) print("尝试重新获取第%d页" % page_num) return parse_page(page_num)# 通过bs4解析数据def get_rooms_by_beautifulsoup(): ''' 通过bs4库解析数据 获取直播间的名称,观看人数,标签,主播名 ''' wait.until(EC.presence_of_element_located( (By.CSS_SELECTOR, "ul#live-list-contentbox > li"))) html = driver.page_source soup = BeautifulSoup(html, 'lxml') rooms = soup.select('ul#live-list-contentbox > li') for room in rooms: room_name = room.find( 'h3', attrs={ 'class': 'ellipsis'}).get_text( strip=True) view_count = room.find('span', class_='dy-num fr').text tag = room.find('span', class_='tag ellipsis').text hostname = room.find('span', class_='dy-name ellipsis fl').text #print("房间名: " + room_name + "\t观看人数: " + view_count + "\t标签: " + tag + "\t主播名: " + hostname) yield { 'room_name': room_name, 'view_count': view_count, 'tag': tag, 'hostname': hostname, }def save_to_monogodb(room_list): for room in room_list: try: db[MONGO_TABLE].insert(room) # insert支持插入多条数据 print("mongodb插入数据成功:", room) except PyMongoError as e: print("mongodb插入数据失败:", room, e)if __name__ == '__main__': try: total_pages = get_total_pages() for page_num in range(2, total_pages + 1): parse_page(page_num) except Exception as e: print("出错了", e) finally: # 确保 浏览器能正常关闭 print("共有%d页" % total_pages) driver.close()

更多关于Python相关内容可查看本站专题:《Python Socket编程技巧总结》、《Python正则表达式用法总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

希望本文所述对大家Python程序设计有所帮助。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章