Examples of crawling web content in php to capture Web Content

Source: Internet
Author: User

Examples of crawling web content in php to capture Web Content

Examples of crawling web content in php

Method 1:

Use the file_get_contents Method

$ Url = "http://news.sina.com.cn/c/nd/2016-10-23/doc-ifxwztru6951143.shtml"; $ html = file_get_contents ($ url); // use the following code if Chinese characters are garbled // $ getcontent = iconv ("gb2312", "UTF-8 ", $ html); echo "<textarea style = 'width: 800px; height: 600px; '> ". $ html. "</textarea> ";

The code is very simple. You can understand it at first glance and don't explain it.

Method 2:

Use curl

$url = "http://news.sina.com.cn/c/nd/2016-10-23/doc-ifxwztru6951143.shtml";    $ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);$html = curl_exec($ch);curl_close($ch);echo "<textarea style='width:800px;height:600px;'>".$html."</textarea>";
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

With this code, the final request page can be accessed when the request is redirected. Otherwise, the request result will display the following content:

If you have any questions, please leave a message or go to the community on this site for discussion. Thank you for reading this article and hope to help you. Thank you for your support for this site!

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.