After the URL crawl page title, some websites can't catch, method stupid, seek guidance

Source: Internet
Author: User
Through the URL to crawl the title of the page, some sites do not catch, method stupid, seeking guidance.
This post was last edited by u012716911 on 2013-11-04 11:25:29

The code is my own thinking of writing, I do not know there is no better way. Would you please give me some pointers?
Some sites can be caught, such as Baidu, some sites will not catch, such as the home of the Pacific automobile.


Public Function Set_title ()
{
Get in URL
$url = $_post[' url '];
$url = "www.pcauto.com.cn"; Can't catch it!
A series of curl settings
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, curlopt_header,0);
curl_setopt ($ch, curlopt_encoding, ' gzip ');
curl_setopt ($ch, curlopt_returntransfer,1);
$content _source = curl_exec ($ch);
Curl_close ($ch);

Get the encoded format of captured content



transcoding
$content _source = Iconv ($encode, ' Utf-8//ignore ', $content _source);

Intercept
if (Preg_match ("/(.*?) <\/title>/i ", $content _source, $title))
{
echo $title [1];
}
Else
{
Echo ' pull header failed ';
}
}
Curl Crawl title

Share to:


------Solution--------------------
The problem is a regular match there, you can add an S modifier.
if (Preg_match ("/ <title>(.*?) <\/title>/is ", $content _source, $title))</title>

s If this modifier is set, the dot metacharacters (.) in the pattern matches all characters, including line breaks. Without this setting, no line break is included
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.