当前位置: 首页 > 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.lryc.cn/news/578164.html

相关文章:

  • 在vue当中使用动画
  • Hily×亚矩云手机:社交元宇宙的“云端心跳加速器”
  • JVM 垃圾回收(GC)笔记
  • LLaMA-Factory框架之参数详解
  • Webpack原理剖析与实现
  • 1.1_2 计算机网络的组成和功能
  • FastDFS分布式储存
  • 华为云Flexus+DeepSeek征文 | ​​接入华为云ModelArts Studio大模型 —— AI智能法务解决方案革新法律实践​
  • 38.docker启动python解释器,pycharm通过SSH服务直连
  • ERP系统Bug记录
  • 前端Vue面试八股常考题(一)
  • 中证500股指期货一手多少钱呢?风险如何?
  • HTML5 实现的圣诞主题网站源码,使用了 HTML5 和 CSS3 技术,界面美观、节日氛围浓厚。
  • 华为云 Flexus+DeepSeek 征文|基于 Dify 平台开发智能客服 AI Agent 的完整实战指南
  • 【STM32HAL-第1讲 基础篇-单片机简介】
  • 前端开发面试题总结-原生小程序部分
  • 《从量子奇境到前端优化:解锁卡西米尔效应的隐藏力量》
  • 《用奥卡姆剃刀原理,为前端开发“减负增效”》
  • 【软考高项论文】论信息系统项目的整体管理
  • 【Java面试】10GB,1GB内存,如何排序?
  • PHP WebSocket服务器搭建指南
  • 从入门到精通:npm、npx、nvm 包管理工具详解及常用命令
  • Springboot + vue + uni-app小程序web端全套家具商场
  • 【Spring】——事务、整合、注解
  • 设计模式-观察者模式(发布订阅模式)
  • UE5 - 制作《塞尔达传说》中林克的技能 - 17 - 遥控炸弹(二)
  • 键盘第一下无反应
  • 基于Spring Boot的绿园社区团购系统的设计与实现
  • 磁悬浮轴承位移信号的高精度估计:卡尔曼滤波算法深度解析
  • MySQL复杂SQL性能优化实战:多表联查与子查询的高效方法