我想直接从maersk网站上获取信息。举个例子,我正在尝试从这个URL https://www.maersk.com/tracking/221242675中抓取信息--我每天都要在数据库上更新大量的跟踪器,所以我尝试了一些自动操作。
但是,如果有下面的代码,但是它说需要JS才能工作。我甚至连卷发等都试过了,但都没有用。有人知道别的办法吗?
我尝试了以下代码:
<?php
// ------------ teste 14 ------------
$html = file_get_contents('https://www.maersk.com/tracking/#tracking/221242675'); //get the html returned from the following url
echo $html;
$ETAupdate = new DOMDocument();
libxml_use_internal_errors(TRUE); //disable libxml errors
if(!empty($html)){ //if any html is actually returned
$ETAupdate->loadHTML($html);
libxml_clear_errors(); //remove errors for yucky html
$ETA_xpath = new DOMXPath($ETAupdate);
//get all the h2's with an id
$ETA_row = $ETA_xpath->query('//strong');
if($ETA_row->length > 0){
foreach($ETA_row as $row){
echo $row->nodeValue . "<br/>";
}
}
}
?>发布于 2022-11-17 15:41:48
您需要直接从他们的API请求中抓取数据,而不是直接抓取页面URL (除非您使用的是木偶师之类的东西,但对于这个简单的任务,我真的不建议这样做)
我查看了这个站点,API端点是:
https://api.maersk.com/track/221242675?operator=MAEU
这将返回一个JSON格式的响应,您可以解析并使用该响应提取细节。它还将为您提供一个访问数据的更简单的方法,而不是解析HTML。下面的例子。
{
"tpdoc_num": "221242675",
"isContainerSearch": false,
"origin": {
"terminal": "YanTian Intl. Container Terminal",
"geo_site": "1PVA2R05ZGGHQ",
"city": "Yantian",
"state": "Guangdong",
"country": "China",
"country_code": "CN",
"geoid_city": "0L3DBFFJ3KZ9A",
"site_type": "TERMINAL"
},
"destination": {
"terminal": "DCT Gdansk sa",
"geo_site": "02RB4MMG6P32M",
"city": "Gdansk",
"state": "",
"country": "Poland",
"country_code": "PL",
"geoid_city": "3RIGHAIZMGKN3",
"site_type": "TERMINAL"
},
"containers": [ ... ]
}https://stackoverflow.com/questions/74478156
复制相似问题