php Laravel-9中的Scraper

6ojccjat  于 2023-04-10  发布在  PHP
关注(0)|答案(1)|浏览(117)

我为我的项目工作,我刮形式不同的皮肤网站,你可以看到哪些有更便宜的皮肤。所以我做了我的第一个刮刀,它不刮。可以使用一些帮助。

use Illuminate\Http\Request;
use Goutte\Client;

class CsmoneyController extends Controller
{
    public function index(Request $request)
    {
        $client = new Client();

        $crawler = $client->request('GET', 'https://cs.money/market');
        $skins = $crawler->filterXPath('.BuyPage_body__27jeL')->each(function ($node) {
            $name = $node->filterXPath('.market-item__name')->text();
            $price = $node->filterXPath('//div[contains(@class, "styles_price__1m7op")]/span[contains(@class, "price_currency__1RmBq")]')->text();
            $image = $node->filterXPath('//img[@class="BaseCard_image_content__2GmuD"]')->attr('src');
            $float = $node->filterXPath('.csm_ui__text__6542e csm_ui__body_12_regular__6542e CSGODescription_description__3cUg_ large')->eq(0)->text();
            $condition = $node->filterXPath('.market-item__params .param')->eq(1)->text();
            return [
                'name' => $name,
                'price' => $price,
                'image' => $image,
                'float' => $float,
                'condition' => $condition
            ];
        });

        return view('scraper', ['skins' => $skins]);
    }
}
rjjhvcjd

rjjhvcjd1#

根据您提供的代码,我可以看到您正在使用Goutte库从www.example.com网页中抓取数据cs.money/market。但是,我注意到您在filterXPath()函数中使用了不正确的类选择器。
不使用CSS类选择器.BuyPage_body__27jeL,您应该使用XPath选择器来定位正确的元素。以下是如何更新代码:

$skins = $crawler->filterXPath('//div[contains(@class, "market_item_row")]')->each(function ($node) {
$name = $node->filterXPath('.//div[contains(@class, "market_item_name")]')->text();
$price = $node->filterXPath('.//div[contains(@class, "market_item_price")]')->text();
$image = $node->filterXPath('.//img[contains(@class, "market_item_img")]/@src')->text();
$float = $node->filterXPath('.//div[contains(@class, "market_item_description")]/div[contains(@class, "csm_flex")]/div[1]')->text();
$condition = $node->filterXPath('.//div[contains(@class, "market_item_description")]/div[contains(@class, "csm_flex")]/div[2]')->text();
return [        'name' => $name,        'price' => $price,        'image' => $image,        'float' => $float,        'condition' => $condition    ];
});

在这个更新的代码中,我使用contains()函数来定位包含指定CSS类的元素。我还使用//符号来搜索DOM树中的任何位置的元素。
我希望这能帮助你让你的刮刀工作。

相关问题