使用Php CUrl和正则表达式的数据提取 [英] Data Fetch Using Php CUrl and Regular Expressions

查看:160
本文介绍了使用Php CUrl和正则表达式的数据提取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望使用php curl从一个网页获取信息,我使用php正则表达式过滤数据以匹配标记,但不工作。

I wish to fetch information from one webpage using php curl, I am filtering data using php regular expression to match tag, but not working.

这里是网页< a href =http://vikramshopping.com/reallife-3in1-printscriptcopy-5 =nofollow>点击此处

这里是我的php code

here is my php code

if(preg_match('/<div class="price-gruop"><span class="text-price">Price:<\/span>(.*?)<\/div>/', get_page($url),$matches2))
        {
       $matches2[1] = strtolower($matches2[1]);
       $data['price']=$matches2[1];

        }

function get_page($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
//curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_HEADER, 0); // return headers 0 no 1 yes
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return page 1:yes
curl_setopt($ch, CURLOPT_TIMEOUT, 200); // http request timeout 20 seconds
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Follow redirects, need this if the url changes
curl_setopt($ch, CURLOPT_MAXREDIRS, 2); //if http server gives redirection responce
curl_setopt($ch, CURLOPT_USERAGENT,
    "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt"); // cookies storage / here the changes have been made
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // false for https
curl_setopt($ch, CURLOPT_ENCODING, "gzip"); // the page encoding

$data = curl_exec($ch); // execute the http request
curl_close($ch); // close the connection
return $data;
}

我得到字符串null值。请告诉我如何在标签之间获取价值。

I am getting string null value. please tell me how to get value in between tags.

推荐答案

使用 code> 从此处下载 simple_html_dom.php 链接

$url = "http://vikramshopping.com/reallife-3in1-printscriptcopy-5";
// Include the library
include('simple_html_dom.php');

// Retrieve the DOM from a given URL
$html = file_get_html($url);

这是您需要我了解的
$ b

it is what you need that I understand

// Find all DIV tags that have a class of "price-gruop" 
foreach($html->find('div.price-gruop') as $e) {
    echo $e->outertext . '<br>';
}

或与 preg_match

$html = '<div class="price-gruop">
                            <span class="text-price">Price:</span>
                                                        INR135.00                                                   </div>';
if(preg_match('/<div class="price-gruop">\s*<span class="text-price">\s*Price:\s*<\/span>\s*(.*)\s*<\/div>/', $html,$matches))
echo '<pre>';print_r("Price: ".$matches[1]);echo '</pre>';

Demo with preg_match

也可以使用下面的其他示例

also you can use other examples from below

// Find all "A" tags and print their HREFs
foreach($html->find('a') as $e)
    echo $e->href . '<br>';

// Retrieve all images and print their SRCs
foreach($html->find('img') as $e)
    echo $e->src . '<br>';

// Find all images, print their text with the "<>" included
foreach($html->find('img') as $e)
    echo $e->outertext . '<br>';

// Find the DIV tag with an id of "myId"
foreach($html->find('div#myId') as $e)
    echo $e->innertext . '<br>';

// Find all SPAN tags that have a class of "myClass"
foreach($html->find('div.myClass') as $e)
    echo $e->outertext . '<br>';

// Find all TD tags with "align=center"
foreach($html->find('td[align=center]') as $e)
    echo $e->innertext . '<br>';

// Extract all text from a given cell
echo $html->find('td[align="center"]', 1)->plaintext.'<br><hr>';

这篇关于使用Php CUrl和正则表达式的数据提取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆