<?php
include_once('simple_html_dom.php');
$veri = file_get_html("http://apps.istanbulsaglik.gov.tr/Eczane");
preg_match_all('@<a href="(.*?)" class="ilce-link" data-value="(.*?)"
data-ilcename="(.*?)" data-title="(.*?)" id="ilce" title="(.*?)"><i
class="fa fa-dot-circle-o"></i>(.*?)</a>@si',$veri,$baslik);
$length = count($baslik[4]);
for ($i = 0; $i < $length; $i++) {
echo $baslik[4][$i];
echo "</br>";
}
preg_match_all('@<table class="table ilce-nobet-detay" id="ilce-nobet-detay">(.*?)</table>@si',$veri,$adres);
echo $adres[1][1];
?>
在此链接中; http://apps.istanbulsaglik.gov.tr/Eczane我无法获得将在“Eczaneler”下列出的右侧元素。
因为我需要单击任何左侧元素,所以我可以看到它们。我想做的就是在我的网络爬虫中获取这些元素。 主要问题是如何让我的爬虫点击?不点击我看不到任何数据。 如果我可以让它点击,那么我就可以从 html 源获取数据。如果不是,我的爬虫将始终返回空。
最佳答案
如果您在http://apps.istanbulsaglik.gov.tr/Eczane上使用任何浏览器的检查器链接,您将看到 ılçeler
列中的每个链接都有一个数据值并绑定(bind)到一个点击事件:
页面Javascript代码:
$(function () {
$(".ilce-link").on("click", function (parameters) {
var title = $(this).data("title").toUpperCase();
var id = $(this).data("value");
var request = $.ajax({
url: "/Eczane/nobetci",
method: "POST",
data: { "id": id, "token": "aa416735d12fd44b" },
dataType: "html"
});
request.done(function (data) {
$("#nobet").empty(" ");
$("#nobet").html('<i class="fa fa-spinner fa-spin"></i>');
$("#nobet").html(data);
document.title = "06-11-2017 TARİHİNDEKİ " + title + " İLEÇSİNDEKİ NÖBETÇİ ECZANE LİSTESİ";
});
});
});
此代码意味着当您单击左栏中的任何链接时,脚本将通过 AJAX 向此网址创建一个 POST 请求:http://apps.istanbulsaglik.gov.tr/Eczane/nobetci带有 id 和 token 。
所以思路就是直接使用这个url来post数据,可以从link元素中获取id,从第一页的js代码中获取token,然后使用CURL PHP发布这些数据。
以下是使用 CURL post 的示例:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://apps.istanbulsaglik.gov.tr/Eczane/nobetci");
curl_setopt($ch, CURLOPT_POST, 1);
// you can use preg_match_all to retrieve the id and the token from the first page
curl_setopt($ch, CURLOPT_POSTFIELDS, "id=$id&token=$token");
$output = curl_exec ($ch);
curl_close ($ch);
关于javascript - PHP网络爬虫点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47128499/