Home >Backend Development >PHP Tutorial >How to collect article pages of WeChat public accounts using PHP

How to collect article pages of WeChat public accounts using PHP

韦小宝
韦小宝Original
2017-12-16 14:11:093626browse

Let me analyze how to use PHP to write a method of collecting WeChat public account articles and PHP code will be explained in detail. Friends who are interested in PHP can learn it.

There are several problems in collecting historical messages of public accounts through Sogou search:

1. There is a verification code;

2. The historical message list only has the latest 10 pieces of bulk content;

3. The article address has a validity period;

4. It is said that you need to change the IP for batch collection;

There is no way through the method in my previous article For these problems, although the construction of the collection system is not as simple as writing rules for traditional collectors to crawl. However, the efficiency of batch collection after being set up once is still acceptable. Moreover, the collected article addresses are permanently valid, and all historical messages of a public account can be collected.
Let’s start with the link address of a public account article:

1. Copy the link address from the menu in the upper right corner of WeChat:

http:/ /mp.weixin.qq.com/s/fF34bERZ0je_8RWEJjoZ5A

2. The address obtained from the historical message list:

http://mp. weixin.qq.com/s?biz=MjM5NDAwMTA2MA==&mid=2695729619&idx=1&sn=8be0b6bd0210cee0d492ebdf20f7371f&chksm=83d74818b4a0c10ef286b33bb7deb73226125f866ddb 5b2781166066a69afef3705eabdb3b85&scene=4#wechat_redirect

3. Complete real address:

Https: //##mp.weixin.qq.com/s? biz = mjm5ndawmta2ma == & mid = 2695729619 & IDX = 1 & SN = 8BE0B6BD0210DF20F7371F & C & C HKSM = 83D74818B4A0C10EF286B33bb7Deb73226125F866DDB5B2781166069AFEF3705EABDB3B85 & Scene = 37 & KEY = C81D77727118A0E6CE32BE2D 9DCAA2A7436AEBA2C1D47A20D02194D1C944A8286A8EDED93495EEADD05DA4 12 BBFAA638A379750AAA4CF5C00E4D785710D9736C72770A57775a5a5a5a5a5a5a5a57a5 15C23FF2400 & Ascene = 3 & uin = mzuyotiynq%3d%3D & DeviceType =iOS10.1.1&version=16050120&nettype=WIFI&fontScale=100&pass_ticket=FGRyGfXLPEa4AeOsIZu7KFJo6CiXOZex83Y5YBRglW4%3D&wx_header=1

The above 3 addresses are the addresses of the same article. If you obtain them from different locations, you will get completely different 3 addresses. results.

Like the historical message page, WeChat has a mechanism for automatically supplementing parameters. The first address is obtained by copying the link and seems to be a disguised encoding. In fact, it’s useless and we won’t consider it. The second address is the link address obtained from the json article list of historical messages through the method introduced in the previous article. We can save this address to the database. Afterwards, the article content can be obtained from the server through this address. After the parameters are added to the third link, the purpose is to allow the reading js in the article page to obtain the json result of the reading likes. In the method of our previous article, the article page is opened and displayed by the client. Because of these parameters, the js in the article page automatically obtains the reading volume, so we can obtain the reading volume of this article through the proxy service. .

The content of this article is to study in detail how to obtain article content and other useful information based on the method introduced in the previous article of this column.

(list of articles saved in my database, some fields)

1. Get the source code of the article:

You can read the article source code into a variable through the PHP function file_get_content(). Since the source code of the WeChat article can be opened from the browser, I will not paste it here to avoid wasting page space.


<?
//$content_url 变量的值为文章地址
$html = file_get_contents($content_url);
?>


2. Useful information in the source code:

1) Original content :

The original content is contained in a cfd680454c127ace0ad40a8afa2cb84a94b3e26ee717c64999d7867364b1b4a3 tag, obtained through the php code:


<?
preg_match_all("/id=\"js_content\">(.*)<script/iUs",$html,$content,PREG_PATTERN_ORDER);
$content = "<p id=&#39;js_content&#39;>".$content[1][0];
?>


The beginning of the regular pattern identifies cfd680454c127ace0ad40a8afa2cb84a, and the end identifies 673074b18359cfecd0bc16db102231c4;My regular matching level is limited, so I can only write it like this. I hope someone with expertise can point out a better regular matching method.

Also note: This matching rule may change after a period of time. This article will be kept updated as much as possible. If you make a collection system based on my article and it fails one day, don’t forget to come back and check if the article has been updated.

2) Content processing:


Through the above method, we obtained the html of the article content, but after you display the article content, you will find that the pictures and videos cannot be displayed normally. . Because this html still needs some processing:

首先是图片,微信文章中的a1f02c36ba31691bcfe87b2722de723b标签中的src属性全部都用了src属性代替。只有在显示的时候才会被替换过来。所以我们也有两个方案,将源代码直接替换过来,或者用js在显示时候再替换。下面我先介绍直接替换html的方法:


<?
//$content变量的值是前面获取到的文章内容html
$content = str_replace("src","src",$content);
?>


然后是视频,视频的显示不正常,经过长期测试后发现只要替换一个页面地址就能解决,过程就不说了,直接说结果:


<?
//$content变量的值是前面获取到的文章内容html
$content = str_replace("preview.html","player.html",$content);
?>


通过这两个替换之后,文章内容html中的图片和视频就都正常了。

3) 公众号相关信息:

通过本专栏之前的文章,介绍了我们使用微信客户端,任意打开一个公众号的历史消息页之后。系统从数据库中识别biz的值,发现数据库中没有记录,就会插入一条新的纪录。之后的采集队列就会定期根据这个biz来获取这个公众号的历史消息列表。

但是我们只获得了这个公众号的biz,公众号的名称,头像这两个重要信息还是没有获取到。主要原因是历史消息页面中没有这两个信息。但是我们可以从文章页面中获取到。

在微信文章页面html的底部,有一些js的变量赋值的代码,通过正则匹配之后我们就可以获得这两个公众号的信息:


<?
//$html变量的值是前面获取到的文章全部html
preg_match_all(&#39;/var nickname = \"(.*?)\";/si&#39;,$html,$m);
$nickname = $m[1][0];//公众号昵称
preg_match_all(&#39;/var round_head_img = \"(.*?)\";/si&#39;,$html,$m);
$head_img = $m[1][0];//公众号头像
?>


通过这两个正则匹配,我们就能获取到公众号的头像和昵称,然后根据文章地址中的biz,可以保存到对应的微信号数据表中。

3、文章的保存和处理

前面的代码已经将文章内容获取到变量中了。如何保存其实每个人也许都有自己的想法。我这里介绍一下我的保存内容的方法:

将文章内容的html以数据库id为文件名保存成html文件,以biz字段为目录。


<?
$dir = "./".$biz."/";
$filename = $dir.$id.".html";
if(!is_dir($dir)) {
  mkdir($cache_dir);
  chmod($cache_dir,0777);
}
$file = fopen($filename, "w");
fwrite($file, $content);
fclose($file);
?>


以上代码是一个标准的php建立文件夹保存文件的代码,大家可以根据自己的实际情况安排保存方法。

在这之后我们就可以在自己的服务器上得到一个html文件,内容就是公众号的文章内容。我们可以从浏览器中打开看一下。这时你也许会发现图片防盗链了!无法正常显示!包括数据库中保存的文章封面图,公众号的头像都是防盗链的。

别急,这个问题很好解决,只需要将图片也保存到自己的服务器,无非是将来会占用自己的服务器空间和带宽。

图片防盗链的原理是当图片在网页中显示的时候,图片服务器会检测到引用这张图片的服务器域名,当发现服务器域名不包含http://qq.com或http://qpic.cn的时候就会被替换成防盗链图片。

但是如果检测不到引用页面的域名就会正常显示,所以我们通过php的函数file_get_content()就可以将图片的二进制代码获取过来,然后根据自己的想法起个文件名保存到自己的服务器上。在这里再介绍一个保存图片的方法,我目前使用了腾讯云的“万象优图”,通过它们提供的api将图片保存到云空间,这样的好处是读取图片时直接在图片的链接地址加上希望得到的图片尺寸大小参数,就可以直接得到一张缩略图。比存在自己的服务器方便得多。阿里云也应该有同样的产品,好像名叫对象存储。

另外,我采集公众号内容的目的是制作成一个新闻app,在app中将html代码显示出来之后,因为app同样没有域名,防盗链服务器也同样不会认为图片被盗链了。这样就可以直接显示图片出来。

以上就是我总结的公众号文章内容的采集与存储方法,希望能够帮到你。

相关推荐:

PHP微信红包实现代码介绍

PHP微信PC二维码登陆的实现方法

PHP微信开发用Cache 解决数据缓存

The above is the detailed content of How to collect article pages of WeChat public accounts using PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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