How to solve the garbled problem of php web crawling

藏色散人
Release: 2023-03-05 18:34:01
Original
2886 people have browsed it

Solution to php web page crawling garbled code: 1. Use "mbconvertencoding" to convert encoding; 2. Set and add "curl_setopt($ch, CURLOPT_ENCODING, 'gzip');" option; 3. Add header at the top code.

How to solve the garbled problem of php web crawling

Recommended: "PHP Video Tutorial"

php crawling page garbled code

The solution to garbled characters similar to ������� appears when crawling the page is as follows

1. Convert encoding

str=mbconvertencoding(str, “utf-8”, “GBK”);
Copy after login

2. Data is compressed by gzip

When curl obtains data, add the following options:

curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
Copy after login

Using the file_get_contents function requires installing the zlib library

$data = file_get_contents("compress.zlib://".$url);
Copy after login

3. After obtaining the data, garbled characters are displayed

at the top Add the following code

header("Content-type: text/html; charset=utf-8");
Copy after login

The above is the detailed content of How to solve the garbled problem of php web crawling. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!