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

Python + Selenium 自动化爬取途牛动态网页

1. 引言

在互联网数据采集领域,动态网页(即通过JavaScript异步加载数据的网页)的爬取一直是一个挑战。传统的**<font style="color:rgb(64, 64, 64);background-color:rgb(236, 236, 236);">requests</font>**+**<font style="color:rgb(64, 64, 64);background-color:rgb(236, 236, 236);">BeautifulSoup</font>**组合适用于静态页面,但对于动态渲染的内容(如途牛旅游网的酒店、景点、评论等)则难以直接获取。

Selenium 是一个强大的浏览器自动化工具,可以模拟用户操作(如点击、滚动、输入等),并获取动态渲染后的完整HTML。本文将详细介绍如何使用 Python + Selenium 自动化爬取途牛旅游网的动态数据,并提供完整的代码实现。

2. 环境准备

在开始之前,我们需要安装必要的Python库:

此外,Selenium需要浏览器驱动(如ChromeDriver)。请确保已安装 Chrome浏览器,并下载对应版本的 ChromeDriver(下载地址)。

3. Selenium基础操作

3.1 初始化浏览器驱动

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time# 配置ChromeDriver路径
driver_path = "你的ChromeDriver路径"  # 例如:/usr/local/bin/chromedriver
service = Service(driver_path)# 启动浏览器(无头模式可选)
options = webdriver.ChromeOptions()
options.add_argument('--headless')  # 无头模式,不显示浏览器窗口
driver = webdriver.Chrome(service=service, options=options)

3.2 访问网页并等待加载

url = "https://www.tuniu.com/"
driver.get(url)
time.sleep(3)  # 等待页面加载

3.3 查找元素并交互

Selenium提供多种元素定位方式:

  • **<font style="color:rgb(64, 64, 64);background-color:rgb(236, 236, 236);">find_element(By.ID, "id")</font>**
  • **<font style="color:rgb(64, 64, 64);background-color:rgb(236, 236, 236);">find_element(By.CLASS_NAME, "class")</font>**
  • **<font style="color:rgb(64, 64, 64);background-color:rgb(236, 236, 236);">find_element(By.XPATH, "xpath")</font>**

例如,搜索“北京”旅游线路:

search_box = driver.find_element(By.ID, "search-input")
search_box.send_keys("北京")
search_box.send_keys(Keys.RETURN)  # 模拟回车
time.sleep(5)  # 等待搜索结果加载

4. 爬取途牛旅游数据实战

4.1 目标分析

假设我们要爬取途牛旅游网的 热门旅游线路,包括:

  • 线路名称
  • 价格
  • 出发地
  • 行程天数
  • 用户评分

4.2 获取动态渲染的HTML

由于途牛的数据是动态加载的,直接**<font style="color:rgb(64, 64, 64);background-color:rgb(236, 236, 236);">requests.get()</font>**无法获取完整HTML。使用Selenium获取渲染后的页面:

tifulSoup)

from bs4 import BeautifulSoup
import pandas as pdsoup = BeautifulSoup(html, 'html.parser')tours = []
for item in soup.select('.trip-item'):  # 根据实际HTML结构调整选择器name = item.select_one('.title').text.strip()price = item.select_one('.price').text.strip()departure = item.select_one('.departure').text.strip()days = item.select_one('.days').text.strip()rating = item.select_one('.rating').text.strip()tours.append({'name': name,'price': price,'departure': departure,'days': days,'rating': rating})# 存储为DataFrame
df = pd.DataFrame(tours)
print(df.head())

4.4 翻页爬取

途牛旅游数据通常是分页加载的,我们可以模拟点击“下一页”:

while True:try:next_page = driver.find_element(By.CSS_SELECTOR, '.next-page')next_page.click()time.sleep(3)  # 等待新页面加载html = driver.page_source# 继续解析...except:break  # 没有下一页时退出

5. 反爬策略应对

途牛可能会检测Selenium爬虫,常见的反反爬措施:

修改User-Agent

禁用自动化标志

使用代理IP

随机等待时间

6. 完整代码示例

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
import pandas as pd
import time
import random# 代理配置
proxyHost = "www.16yun.cn"
proxyPort = "5445"
proxyUser = "16QMSOML"
proxyPass = "280651"# 初始化浏览器
driver_path = "你的ChromeDriver路径"
service = Service(driver_path)
options = webdriver.ChromeOptions()# 设置代理
proxy_options = f"--proxy-server=http://{proxyUser}:{proxyPass}@{proxyHost}:{proxyPort}"
options.add_argument(proxy_options)# 其他选项
options.add_argument('--headless')  # 无头模式
options.add_argument('user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36')# 绕过代理认证弹窗(如果需要)
options.add_argument('--proxy-bypass-list=*')
options.add_argument('--ignore-certificate-errors')driver = webdriver.Chrome(service=service, options=options)# 访问途牛旅游网
url = "https://www.tuniu.com/"
driver.get(url)
time.sleep(3)# 搜索"北京"旅游线路
search_box = driver.find_element(By.ID, "search-input")
search_box.send_keys("北京")
search_box.send_keys(Keys.RETURN)
time.sleep(5)# 爬取多页数据
tours = []
for _ in range(3):  # 爬取3页html = driver.page_sourcesoup = BeautifulSoup(html, 'html.parser')for item in soup.select('.trip-item'):name = item.select_one('.title').text.strip()price = item.select_one('.price').text.strip()departure = item.select_one('.departure').text.strip()days = item.select_one('.days').text.strip()rating = item.select_one('.rating').text.strip()tours.append({'name': name,'price': price,'departure': departure,'days': days,'rating': rating})# 翻页try:next_page = driver.find_element(By.CSS_SELECTOR, '.next-page')next_page.click()time.sleep(random.uniform(2, 5))except:break# 存储数据
df = pd.DataFrame(tours)
df.to_csv('tuniu_tours.csv', index=False, encoding='utf-8-sig')# 关闭浏览器
driver.quit()
print("数据爬取完成,已保存至 tuniu_tours.csv")

7. 总结

本文介绍了如何使用 Python + Selenium 自动化爬取途牛旅游网的动态数据,包括:

  1. Selenium基础操作(启动浏览器、查找元素、模拟点击)
  2. 动态页面解析(结合BeautifulSoup提取数据)
  3. 翻页爬取(自动点击“下一页”)
  4. 反爬策略(User-Agent、代理IP、随机等待)

Selenium虽然强大,但速度较慢,适合小规模爬取。如需更高效率,可研究 PlaywrightScrapy + Splash 方案。

http://www.dtcms.com/a/263387.html

相关文章:

  • Qt Quick 与 QML(四)qml中的Delegate系列委托组件
  • 七天学会SpringCloud分布式微服务——05——OpenFeign
  • 基于时间策略+应用过滤的游戏防沉迷方案:技术实现与工具推荐
  • Python pandas-profiling 详解:一键生成数据分析报告的利器
  • 使用自定义注解完成redis缓存
  • Windows Excel文档办公工作数据整理小工具
  • SpringCloud系列(43)--搭建SpringCloud Config客户端
  • Install Ubuntu 24.04 System
  • SpringCloud系列(42)--搭建SpringCloud Config分布式配置总控中心(服务端)
  • ProPlus2024Retail 安装教程(详细步骤+激活方法)- 最新版安装包下载与使用指南
  • mysql运维语句
  • window显示驱动开发—在注册表中设置 DXGI 信息
  • SCAU期末笔记 - 操作系统 选填题
  • 【机器学习第四期(Python)】LightGBM 方法原理详解
  • 跨主机用 Docker Compose 部署 PostgreSQL + PostGIS 主从
  • [特殊字符]【联邦学习实战】用 PyTorch 从 0 搭建一个最简单的联邦学习系统(含完整代码)
  • 编程新手之环境搭建:node python
  • [论文阅读] Neural Architecture Search: Insights from 1000 Papers
  • 创客匠人解析知识变现赛道:从 IP 孵化到商业闭环的核心策略
  • xilinx axi datamover IP使用demo
  • 【STM32HAL-第1讲 基础篇-单片机简介】
  • C#数字格式化全解析:从基础到进阶的实战指南
  • 腾讯云空间,高性能显卡云,安装xinference报错,pip install 空间不够用了
  • leedcode:找到字符串中所有字母异位词
  • 04密码加密
  • 中钧科技参加中亚数字经济对话会,引领新疆企业数字化新征程!
  • 【Teensy】在ArduinoIDE中配置Teensy4.1
  • LoRA 实战指南:NLP 与 CV 场景的高效微调方法全解析
  • 非常详细版: dd.device.geolocation 钉钉微应用获取定位,移动端 PC端都操作,Vue实现钉钉微应用获取精准定位并渲染在地图组件上
  • 强化学习概述及学习流程