【问题标题】:Goutte crawler get styleGoutte履带式获得风格
【发布时间】:2021-01-18 19:10:04
【问题描述】:

我正在使用 Goutte 爬虫。 因此,在几个小时内,我试图在具有特定查询的搜索页面中获取 div 的样式属性,并且该样式具有 Background-img。 所以首先我通过

向url发出了一个GET请求
   $crawler = $client->request('GET',"https://www.esheeq.net/search/مسلسل+علي+رضا");

然后爬过去

$crawler->filter(".imgBg")->attr("style")

并打印出来,它工作了,但问题是当我更改搜索查询(例如(https://www.esheeq.net/search/مسلسل+الغرفة+الحمراء)时,它会抛出一个错误

Fatal error: Uncaught InvalidArgumentException: The current node list is empty. in C:\xampp\htdocs\esheeqAPI\vendor\symfony\dom-crawler\Crawler.php:550 Stack trace: #0 C:\xampp\htdocs\esheeqAPI\api\functions.php(8): Symfony\Component\DomCrawler\Crawler->attr('style') #1 C:\xampp\htdocs\esheeqAPI\api\tests.php(4): InsertMultipleSeries() #2 {main} thrown in C:\xampp\htdocs\esheeqAPI\vendor\symfony\dom-crawler\Crawler.php on line 550

但是当我打开 URL 时,我要求它显示一个具有样式属性的 imgBg 类的 div。那为什么我会出错,我该如何解决。

【问题讨论】:

    标签: php web-scraping goutte domcrawler


    【解决方案1】:

    也许可以尝试这样做:

    use Symfony\Component\DomCrawler\Crawler;
    
    // you need to urlencode arabic characters, because php doesn't do that automatically
    
    $url = "https://www.esheeq.net/search/" . urlencode( "مسلسل+الغرفة+الحمراء" );
    
    $html_content = file_get_contents($url);
    
    // and then;
    $crawler = new Crawler( $html_content );
    
    $crawler->filter(".imgBg")->attr("style");
    

    如果它不起作用,请告诉我

    【讨论】:

    猜你喜欢
    • 2016-05-15
    • 2011-03-13
    • 1970-01-01
    • 2017-09-09
    • 2021-11-05
    • 1970-01-01
    • 1970-01-01
    • 2019-11-11
    • 2015-10-22
    相关资源
    最近更新 更多