惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy International News Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Troy Hunt's Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Attack and Defense Labs
Attack and Defense Labs
S
Secure Thoughts
V2EX - 技术
V2EX - 技术
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
O
OpenAI News
Cloudbric
Cloudbric
Google Online Security Blog
Google Online Security Blog
Schneier on Security
Schneier on Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Help Net Security
Help Net Security
Cyberwarzone
Cyberwarzone
G
GRAHAM CLULEY
L
Lohrmann on Cybersecurity
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Spread Privacy
Spread Privacy
NISL@THU
NISL@THU
N
News and Events Feed by Topic
T
Tenable Blog
S
Security @ Cisco Blogs
N
News and Events Feed by Topic
The Hacker News
The Hacker News
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
V
Visual Studio Blog
P
Proofpoint News Feed
Webroot Blog
Webroot Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 三生石上(FineUI控件)
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
L
LangChain Blog
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东

个人技术分享

GPT指令详细资料 – 个人技术分享 php操作cookie 转换数组形式,可取某一个值 – 个人技术分享 php操作cookie字符串,替换对应值 – 个人技术分享 php 操作cookie值 – 个人技术分享 js 引入vconsole – 个人技术分享 fastadmin appendfieldlist 用法 – 个人技术分享 PHP 转到 Go – 个人技术分享 解决移动设备上的所有浏览器顶部都有地址栏,导致高度问题 – 个人技术分享 Python框架 Flask和Django – 区别 – 个人技术分享 PHP To Go 转型手记 – 个人技术分享 真正的50行css代码实现响应式“瀑布流”布局 – 个人技术分享
PHP使用无头浏览器如何帮助数据提取和抓取 – 个人技术分享
hanhanjun888 · 2024-07-24 · via 个人技术分享

php使用webdriver做采集的好处
自动化:WebDriver 可以模拟用户与浏览器的交互操作,如点击、输入文本、提交表单等。这使得您可以自动化执行各种网页操作,而无需手动进行。
多浏览器支持:WebDriver 支持多种浏览器,包括 Chrome、Firefox、Safari 等。这意味着您可以在不同的浏览器上运行和测试您的采集脚本,以确保在不同环境下的兼容性。
动态内容处理:许多网站使用 JavaScript 和 AJAX 技术来加载和更新内容。通过 WebDriver,您可以等待页面加载完成并获取完整的渲染后的页面内容,包括动态生成的内容。
数据抽取:WebDriver 提供了丰富的方法和选择器来定位和提取页面中的元素。您可以使用这些功能来获取所需数据,并将其保存到文件或数据库中进行进一步处理。
高度可定制化:WebDriver 允许您编写灵活且高度可定制化的采集脚本。您可以根据需要设置各种选项和参数,以适应不同网站和场景。
跨平台支持:WebDriver 是一个跨平台的工具,可以在 Windows、Mac 和 Linux 等操作系统上运行。这使得它成为一个广泛适用的采集解决方案。
使用方法
使用 WebDriver,您可以等待页面加载完成并获取完整的渲染后的页面内容,包括动态生成的内容。这意味着您可以采集到前后端分离网站上通过 JavaScript 或 AJAX 加载的数据。接下来让我自己动手写一个试试看。
安装浏览器chrome
1. 下载 Chrome 浏览器的安装包:
wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
2. 安装 Chrome 浏览器:
sudo yum localinstall google-chrome-stable_current_x86_64.rpm
3. 验证 Chrome 是否成功安装:
google-chrome –version
4. 下载对应版本的 ChromeDriver,并解压到指定目录(例如 /usr/local/bin):
https://googlechromelabs.github.io/chrome-for-testing/ (各个版本的下载地址)
wget https://chromedriver.storage.googleapis.com/114.0.5735.90/chromedriver_linux64.zip
unzip chromedriver_linux64.zip -d /usr/local/bin/
5. 启动 ChromeDriver 服务:
LANGUAGE=ZH-CN.UTF-8 /usr/local/bin/chromedriver –port=9515
6.成功运行后,您应该会看到类似以下输出:
Starting ChromeDriver {version} on port 9515…
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
https://github.com/php-webdriver/php-webdriver
PHP+WebDriver使用教程

https://github.com/php-webdriver/php-webdriver

php代码执行

<?php
require_once('vendor/autoload.php');
use Facebook\WebDriver\Remote\DesiredCapabilities;

use Facebook\WebDriver\Remote\RemoteWebDriver;

use Facebook\WebDriver\Chrome\ChromeOptions;

$options = new ChromeOptions();

$options->addArguments(['--no-sandbox','--headless']);


$capabilities = DesiredCapabilities::chrome();

$capabilities->setCapability(ChromeOptions::CAPABILITY, $options);


$host = 'http://localhost:9555';

$driver = RemoteWebDriver::create($host, $capabilities);


// 访问百度网站

$driver->get('https://www.baidu.com');


// 获取页面源代码

$pageSource = $driver->getPageSource();


echo $pageSource;


// 关闭 WebDriver 会话

$driver->quit();