简体   繁体   中英

how to click using simple html dom parse in php

I am trying to click on href attribute Here is my sample html

<ul>
  <li>
     <a href='http://www.google.com'>Google</a>
  </li>
</ul>

And Here is my php code

<?php 
  require_once('classes/simple_html_dom.php');
  $html = file_get_html("/var/www/html/dk/PHP_SCRAPPING/google.html");
  echo $html->find("ul li a",0)->href;
?>

Output is

http://www.google.com

I just want to click on this url. How to do that? Please don't tell me to do this

file_get_html($html->find("ul li a",0)->href);

I am looking for method which can click on any href by using simple html dom.

You can't do that in PHP since all PHP code is executed server side and not client side. The PHP code doesn't run in a browser at all, so in essence there is no link to click.

If you want to script things client side, you'd need to resort to javascript. Although any sane browser won't let you emulate a click in Javascript for security reasons.

I think you need to read up on what exactly you're doing.

看看使用Python和Chrome / Firefox / PhantomJS Webdriver的Selenium,它可以让您完全控制单击任何网页中的任何按钮或链接..使用BeautifulSoup解析它,并且可以通过Python创建我们的json

If i am not getting you wrong you can do it with 2 option

  1. PHP -- header("Location:http://www.google.com");

  2. Javascript-- window.location("http://www.google.com");

Try

echo $html->find("ul li a",0);

If that doesn't works you can do:

echo "<a href='".$html->find("ul li a",0)->href."'>";
echo $html->find("ul li a",0)->plaintext."</a>";

在此API中,他们具有click方法

I would use jQuery, I'm pretty sure you could do something like this.

<head>
<script>      $("#clickable").click();         </script> 
</head>

<body> <?php  echo "<span id='clickable'>" . $html->find("ul li a",0)->href ."</span>";  ?>   </body>

Obviously you would need at add in the jquery library link and (doc.)ready... I haven't tested it but I don't see any reason why jQuery wouldn't click on a server side generated link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM