


Explanation on how to collect article pages of WeChat public accounts using PHP
Let me analyze how to use PHP to write a method of collecting WeChat public account articles and explain the code in detail. Friends who need it can learn from 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 group messages;
3. The article address has a validity period;
4. It is said that batch collection requires changing IP;
There are no such problems through the method in my previous article, although the collection system The construction is not as simple as traditional collectors, just write the rules and crawl them. 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=83d74818b4a0c10ef286b33bb7deb73226125f866ddb5 b2781166066a69afef3705eabdb3b85&scene=4#wechat_redirect
##3. Complete real address:
https://mp.weixin.qq.com/s?__biz=MjM5NDAwMTA2MA==&mid=2695729619&idx=1&sn=8be0b6bd0210cee0d492ebdf20f7371f&chksm=83d74818b4a0c10ef286b33bb7deb7322612 5f866ddb5b2781166066a69afef3705eabdb3b85&scene=37&key=c81d77271180a0e6ce32be2d9dcaa2a7436aeba2c1d47a20d02194d1c944a8286a8eded93495eeadd05da412bbfaa 638a379750aeaa4cf5c00e4d7851c5710d9b9736b80e3c72770a57a515c23ff2400&ascene=3&uin=MzUyOTIyNQ==&devicetype=iOS10.1.1&version=16050120&nettype =WIFI&fontScale=100&pass_ticket=FGRyGfXLPEa4AeOsIZu7KFJo6CiXOZex83Y5YBRglW4=&wx_header=11. 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 tag and is obtained through the php code:<? preg_match_all("/id=\"js_content\">(.*)<script/iUs",$html,$content,PREG_PATTERN_ORDER); $content = "<p id='js_content'>".$content[1][0]; ?>The beginning of the regular pattern identifies
, and the end identifies
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:<? //$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('/var nickname = \"(.*?)\";/si',$html,$m); $nickname = $m[1][0];//公众号昵称 preg_match_all('/var round_head_img = \"(.*?)\";/si',$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同样没有域名,防盗链服务器也同样不会认为图片被盗链了。这样就可以直接显示图片出来。
以上就是我总结的公众号文章内容的采集与存储方法,希望能够帮到你。
相关推荐:
The above is the detailed content of Explanation on how to collect article pages of WeChat public accounts using PHP. For more information, please follow other related articles on the PHP Chinese website!

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

ToretrievedatafromaPHPsession,startthesessionwithsession_start()andaccessvariablesinthe$_SESSIONarray.Forexample:1)Startthesession:session_start().2)Retrievedata:$username=$_SESSION['username'];echo"Welcome,".$username;.Sessionsareserver-si

The steps to build an efficient shopping cart system using sessions include: 1) Understand the definition and function of the session. The session is a server-side storage mechanism used to maintain user status across requests; 2) Implement basic session management, such as adding products to the shopping cart; 3) Expand to advanced usage, supporting product quantity management and deletion; 4) Optimize performance and security, by persisting session data and using secure session identifiers.

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver CS6
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
