当前位置: 首页 > news >正文

初始爬虫11

1.斗鱼selenium爬取

# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
import timeclass Douyu(object):def __init__(self):self.url = 'https://www.douyu.com/directory/all'self.driver = webdriver.Chrome()self.driver.implicitly_wait(10)  # 设置隐式等待,最大等待10秒def parse_data(self):room_list = self.driver.find_elements(By.XPATH, '//*[@id="listAll"]/section[2]/div[2]/ul/li/div')print(len(room_list))data_list = []# 遍历房间列表,从每一个房间节点获取数据for room in room_list:temp = {}# temp['title'] = room.find_element(By.XPATH, './div[2]/div[1]/a').text# temp['type'] = room.find_element(By.XPATH, './div[2]/div[2]/span/a').text# temp['owner'] = room.find_element(By.XPATH, './div[1]/div/a/div/div[2]/div/div[1]/div').text# temp['num'] = room.find_element(By.XPATH, './div[1]/div/a/div/div[2]/div/div[2]/span').texttemp['picture'] = room.find_element(By.XPATH, './div[1]/picture/source[1]').get_attribute('srcset')# print(temp)data_list.append(temp)return data_listdef run(self):self.driver.get(self.url)total_rooms = 0last_count = 0  # 上一次获取的房间数量while True:# 滚动到页面底部self.driver.execute_script('window.scrollTo(0, document.body.scrollHeight);')time.sleep(2)  # 等待页面加载新内容# 获取当前房间数据new_data = self.parse_data()total_rooms += len(new_data)print(f"Total rooms : {total_rooms}")# 检查当前房间数量if total_rooms == last_count:  # 如果新加载的房间数量没有增加,停止滚动print("No more new data to load.")breaklast_count = total_rooms  # 更新最后一次的房间数量print(f"Final total rooms fetched: {total_rooms}")self.driver.quit()  # 退出浏览器if __name__ == '__main__':douyu = Douyu()douyu.run()

2. request+mysql存储

import pymysql
import requests
from lxml import etree# 第一步:请求百度首页并提取内容
url = 'https://www.baidu.com/'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36'
}
response = requests.get(url, headers=headers)
html = etree.HTML(response.content.decode("utf-8"))# 提取文本和链接
classes = ["normal", "c", "color", "t"]
extracted_data = []for cls in classes:xpath_query = f'//div[contains(@class, "{cls}")]'elements = html.xpath(xpath_query)for element in elements:# 提取文本内容text = ''.join(element.xpath('.//text()')).strip()# 提取链接,假定链接是 a 标签的 href 属性link = element.xpath('.//a/@href')link = link[0] if link else "No link found"extracted_data.append((text, link))# 第二步:连接 MySQL 数据库
connection = pymysql.connect(host='localhost',  # 数据库地址user='root',  # MySQL 用户名password='991016',  # MySQL 密码database='test',  # 数据库名称charset='utf8mb4',  # 确保字符集是 utf8mb4cursorclass=pymysql.cursors.DictCursor  # 使用字典形式的游标
)try:with connection.cursor() as cursor:# 创建一个新表存储网页内容create_table_query = """CREATE TABLE IF NOT EXISTS web_content (id INT AUTO_INCREMENT PRIMARY KEY,text_content TEXT,link VARCHAR(255));"""cursor.execute(create_table_query)# 插入提取到的数据insert_query = "INSERT INTO web_content (text_content, link) VALUES (%s, %s)"cursor.executemany(insert_query, extracted_data)# 提交更改connection.commit()# 查询数据并验证是否成功存储cursor.execute("SELECT * FROM web_content")results = cursor.fetchall()for row in results:print(row)finally:connection.close()
http://www.lryc.cn/news/452921.html

相关文章:

  • SSY20241002提高组T4题解__纯数论
  • Python:lambda 函数详解 以及使用
  • 【C++】空指针和野指针
  • 大模型提示词
  • 在线css像素px到Em的转换器
  • 回溯算法解决排列组合及子集问题
  • Unity中Mesh多种网格绘制模式使用方法参考
  • 【Spring Security】基于SpringBoot3.3.4版本②如何配置免鉴权Path
  • 信息学奥赛复赛复习11-CSP-J2020-04方格取数-动态规划、斐波那契数列、最优子结构、重叠子问题、无后效性
  • Hive数仓操作(十二)
  • 计算机毕业设计 基于SpringBoot和Vue的课程教学平台的设计与实现 Java实战项目 附源码+文档+视频讲解
  • 有状态(Session) VS 无状态(Token)
  • 天坑!Spark+Hive+Paimon+Dolphinscheduler
  • JAVA——IO框架
  • 项目管理系统如何实现项目申报流程自动化?
  • ndb9300public-ndb2excel简介
  • C++:const成员
  • 基于ROS的激光雷达点云物体检测
  • 大模型训练环境搭建
  • 使用Java调用GeoTools实现全球国家矢量数据入库实战
  • 计算机毕业设计 基于Python的广东旅游数据分析系统的设计与实现 Python+Django+Vue Python爬虫 附源码 讲解 文档
  • Springboo通过http请求下载文件到服务器
  • 使用CSS实现酷炫加载
  • 【STM32-HAL库】AHT10温湿度传感器使用(STM32F407ZGT6配置i2c)(附带工程下载连接)
  • 深入理解网络通信: 长连接、短连接与WebSocket
  • Linux·环境变量与进程地址空间
  • MYSQL 乐观锁
  • SpringCloud入门(十二)全局过滤器和跨域
  • 51单片机系列-按键检测原理
  • 基于元神操作系统实现NTFS文件操作(五)