【发布时间】:2020-07-07 15:55:07
【问题描述】:
我一直在四处寻找以完成这项工作,但似乎我无法自己完成。我正在使用 cURL 从网站获取一些信息并将这些信息存储在 MySQL 数据库中。我现在拥有的是以下代码:
$target_url = "[http:\[//\]iliria98\[.\]com][1]"; //delete [ and ] to get the url correctly
$userAgent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)';
// make the cURL request to $target_url
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
$html= curl_exec($ch);
if (!$html) {
echo "<br />cURL error number:" .curl_errno($ch);
echo "<br />cURL error:" . curl_error($ch);
exit;
}
// parse the html into a DOMDocument
$document = new DOMDocument();
libxml_use_internal_errors(true);
$document->loadHTML($html);
libxml_clear_errors();
$selector = new DOMXPath($document);
//$anchors = $selector->query('//div[@class="single"]/div[2]');
$anchors = $selector->query('//div[@class="single"]/div');
foreach($anchors as $div) {
$text = $div->nodeValue;
$valuta_arr=explode(',', $text);
var_dump($valuta_arr);
echo $text;
}
而且,输出不正确,因为它从网站获取所有货币代码,但货币值仅来自第一行,来自美元。 我想要的是从指定的 url 上的 html 表中获取值,并将这些值插入到每种货币的数据库中,其中数据库表如下所示:
id
currency
sell
buy
date
我直到 mysql 插入代码才得到,因为我已经挣扎了 3 天,首先从那个网站获取信息。 希望有人可以帮助我。 谢谢大家。
【问题讨论】:
标签: php mysql curl web-scraping php-curl