【问题标题】:How to get only email value from HTML files using DOM ? Only Email value not like link如何使用 DOM 从 HTML 文件中仅获取电子邮件值?只有电子邮件值不像链接
【发布时间】:2021-02-05 05:35:07
【问题描述】:

http://www.frosher.com/schools/acme-academy-burdwan/contact

这是我保存在我的文件夹中的页面链接,并获取他们的地址以及他们学校的所有联系信息。您还可以在 Google 地图块之前看到他们的电子邮件和网络链接。我想获得电子邮件价值。

只需将此 html 页面保存在抓取文件夹中即可。这是我的代码:

<?php 
include('simple_html_dom.php');//Required
$i = 0; 
$dir = 'scraping/';//folder name in which your html file
if ($handle = opendir($dir)) {
    while (($file = readdir($handle)) !== false){
        if (!in_array($file, array('.', '..')) && !is_dir($dir.$file)) 
            $i++;
    }
}
$filenames = array();
foreach(glob('scraping/*.*') as $filename){
    $filenames[] = $filename;//get all files name which are in my folder
}
$i = 1;
foreach($filenames as $val){
    $doc = new DomDocument();
    $doc  = file_get_html($val);
    $ret = $doc->find('div[class=span5]'); 
    foreach($doc->find('.span7') as $element){
        $contact = $element->plaintext;     
        if (preg_match("/\bEmail\b/i", $contact, $match)) {
            $n = 0; // i have used $n for counting because in span7 div their are two a tag so i need only first time value.
            foreach($doc->find('.span7 a') as $element){
                if($n == 0){                    
                    $email = $element; 
                    $n = $n+1;
                }                   
            }           
        }
        else{
            $email = 'Null';
        }           
        echo $email;
        
    }       
    echo '<br/>';
}
?>

这是 php 脚本代码,用文件名保存它,并将 php 文件和抓取文件夹放在公共文件夹中,例如 leo 是放置 php 文件的文件夹,抓取文件夹也在其中。

现在运行 php 文件,您将看到输出。如果没有,那么您还必须在 leo 文件夹中包含“simple_html_dom.php”。

【问题讨论】:

  • 显示一些代码,以便我们看到您尝试了什么?
  • 问题不完整。请简要介绍代码或描述。
  • @Leo 使用“it”中的代码编辑您的问题,而不是在评论中。
  • 帮助您将电子邮件地址收集到垃圾邮件中,不。 p.s phpmyadmin 不是数据库
  • 是的,我可以;谢谢提问

标签: php html email dom


【解决方案1】:

如果你得到整个标签然后尝试关注



foreach($doc->find('.span7 a') as $element){    

    $email = $element; 
    $email = strip_tags($email);
    //now you can check email
}

【讨论】:

  • 它显示“[电子邮件保护] /* */”。如果你们不明白,我可以发送更多代码。或者也许你们给我另一个选择来获得电子邮件价值。@parixit
  • 更新了我的问题请检查@parixit
猜你喜欢
  • 2016-03-22
  • 1970-01-01
  • 2011-04-15
  • 2011-09-03
  • 2016-02-15
  • 1970-01-01
  • 1970-01-01
  • 2021-02-14
  • 2011-09-06
相关资源
最近更新 更多