使用DomCrawler批量本地化淘宝天猫详情图

你没有变强是因为你一直很舒服。
### 安装
1
composer require symfony/dom-crawler
### 代码片段
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// october cms
$crawler = new Crawler($content);
$crawler->filter('img')->each(function (Crawler $node) use($pid) {
$sourceSrc = explode('?',$node->attr('src'))[0];
$sourceSrc = str_replace('https', 'http', $sourceSrc);
if (!starts_with($sourceSrc, 'http')) {
$sourceSrc = "http:".$sourceSrc;
}
$imageManager = new ImageManager(['driver' => 'gd']);
$temPath = storage_path() . '/temp/' .time().basename($sourceSrc);
$imageManager->make($sourceSrc)->save($temPath);
$attachment_type = 'Jason\Ccmall\Models\Product';
$productImage = new File();
$productImage->data = $temPath;
$productImage->field = 'content';
$productImage->attachment_id = $pid;
$productImage->attachment_type = $attachment_type;
$productImage->save();
$node->getNode(0)->setAttribute('src', $productImage->path);
@unlink($temPath);
});
$html = $crawler->html();
Product::query()->where('id', $pid)->update([
'content' => $html
]);
### 实例一
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
use Symfony\Component\DomCrawler\Crawler;

$html = <<<'HTML'
<!DOCTYPE html>
<html>
<body>
<p class="message">Hello World!</p>
<p>Hello Crawler!</p>
</body>
</html>
HTML;

$crawler = new Crawler($html);

foreach ($crawler as $domElement) {
var_dump($domElement->nodeName);
}

实例二

1
2
3
4
5
6
7
8
9
10
$crawler = new Crawler('<html><body /></html>');

$crawler->addHtmlContent('<html><body /></html>');
$crawler->addXmlContent('<root><node /></root>');

$crawler->addContent('<html><body /></html>');
$crawler->addContent('<root><node /></root>', 'text/xml');

$crawler->add('<html><body /></html>');
$crawler->add('<root><node /></root>');

实例三

1
2
3
4
5
$imagesCrawler = $crawler->selectImage('Kitten');
$image = $imagesCrawler->image();

// or do this all at once
$image = $crawler->selectImage('Kitten')->image();

https://symfony.com/doc/current/components/dom_crawler.html