【问题标题】:Crawling a website using Laravel & Elvedia\Goutte: How to extract JSON使用 Laravel 和 Elvedia\Goutte 抓取网站:如何提取 JSON
【发布时间】:2014-08-04 10:35:00
【问题描述】:

我成功地使用Goutte Laravel 4 访问了远程 JSON 资源:

$client = Goutte::getNewClient();

//*
$crawler = $client->request('GET', 'http://domain.mg/admin');

$form = $crawler->selectButton('Login')->form();
$crawler = $client->submit($form, array('username' => 'username', 'password' => 'password'));

//*/

$crawler = $client->request('GET', 'http://domain.mg/usergroup/list'); // Yields JSON Response

return dd($crawler);

它会产生如下输出:

对象(Symfony\Component\DomCrawler\Crawler)#285 (4) { ["uri":protected]=> 字符串(36) "http://domain.mg/usergroup/list" ["defaultNamespacePrefix":"Symfony\Component\DomCrawler\Crawler":private]=> 字符串(7)“默认” ["命名空间":"Symfony\Component\DomCrawler\Crawler":private]=> 数组(0){}[“存储”:“SplObjectStorage”:私有]=>数组(1){ ["0000000075faaa10000000001af55ef8"]=> 数组(2) { ["obj"]=> object(DOMElement)#241 (17) { ["tagName"]=> string(4) "html" ["schemaTypeInfo"]=> NULL ["nodeName"]=> 字符串(4) "html" ["nodeValue"]=> 字符串(438) "[{"id":1,"group_name":"Compte principal","group_desc":"Administrateur","group_level":9},{"id":2,"group_name":"Profil 倒莱斯 comptables","group_desc":"Comptables","group_level":2},{"id":3,"group_name":"Validateur d'op\u00e9ration","group_desc":"主管","group_level":9},{"id":18,"group_name":"否 评论","group_desc":"Autres 雇用\u00e9s","group_level":6},{"id":41,"group_name":"邀请\u00e9","group_desc":"Guest","group_level":2}]" ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(对象值 省略)" ["childNodes"]=> string(22) "(省略对象值)" ["firstChild"]=> string(22) "(省略对象值)" ["lastChild"]=> string(22) "(省略对象值)" ["previousSibling"]=> string(22) "(省略对象值)" ["属性"]=> string(22) "(对象值 省略)" ["ownerDocument"]=> string(22) "(省略对象值)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> 字符串(4) "html" ["baseURI"]=> NULL ["textContent"]=> 字符串(438) "[{"id":1,"group_name":"完成 principal","group_desc":"Administrateur","group_level":9},{"id":2,"group_name":"Profil 倒莱斯 comptables","group_desc":"Comptables","group_level":2},{"id":3,"group_name":"Validateur d'op\u00e9ration","group_desc":"主管","group_level":9},{"id":18,"group_name":"否 评论","group_desc":"Autres 雇用\u00e9s","group_level":6},{"id":41,"group_name":"邀请\u00e9","group_desc":"Guest","group_level":2}]" } ["inf"]=> NULL } } }

我偶然发现在$crawler 对象中提取/转换 JSON 的内部表示。怎么可能呢?

【问题讨论】:

  • 您到底想对 JSON 输出做什么?
  • 我想将远程数据(静态)泵入本地镜像数据库。

标签: php json goutte


【解决方案1】:

钻研Class Symfony\Component\DomCrawler\Crawler文档,我发现

public string html()

    Returns the first node of the list as HTML.

    Return Value

    string  The node html

效果符合我的预期。

return dd($crawler) 转换为return ($crawler->html()) 会产生:

[{"id":1,"group_name":"完成 principal","group_desc":"Administrateur","group_level":9},{"id":2,"group_name":"Profil 倒莱斯 comptables","group_desc":"Comptables","group_level":2},{"id":3,"group_name":"Validateur d'op\u00e9ration","group_desc":"主管","group_level":9},{"id":18,"group_name":"否 评论","group_desc":"Autres 雇用\u00e9s","group_level":6},{"id":41,"group_name":"Invit\u00e9","group_desc":"Guest","group_level":2}]

结论

Goutte 很好地管理了复杂的(Laravel | crsf 机制)登录过程,但我不喜欢使用 html() 条带化 JSON 字符串。

使用return ($crawler->text()) 获得相同的结果在我看来更加“中立”。

【讨论】:

    【解决方案2】:

    我不确定你想用 JSON 做什么,但是将 JSON 字符串转换为数组相当简单:

    $data = json_decode($jsonString);
    

    【讨论】:

    • 那是普通的 PHP。对goutte 不满意,我正在寻找一种惯用的方法来检索至少散布在 $crawler 对象中的 JSON 字符串。
    • 如果您能够从对象中检索 JSON 字符串,为什么不使用json_decode?我不明白这个问题。
    • 绊脚石:如何从$crawler 对象中检索JSON 字符串/JSON? :-(
    • Goutte 在后台使用 CssSelectorDomCrawler。我现在正在研究那些 Symfony 组件。
    • 哦,所以你甚至无法从对象中获取 JSON 字符串,好吧。
    猜你喜欢
    • 2020-06-07
    • 2020-10-26
    • 2021-04-28
    • 2018-10-23
    • 1970-01-01
    • 2018-10-17
    • 2015-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多