Skip to main content

Ruby and Watir libraries crawl the content of specified WeChat public accounts

Ruby and Watir libraries crawl the content of specified WeChat public accounts

Ruby and Watir libraries crawl the content of specified WeChat public accounts

Introduction to Ruby

Ruby is a simple yet powerful object-oriented programming with elegant syntax and powerful functionality. It is a widely used language in areas such as web development, data analysis, and automation tasks. In this article, we will use Ruby and Watir libraries to develop a web crawler for crawling the content of specified WeChat official accounts.

Project demand scenario

Suppose we need to obtain the article content of a specific WeChat public account for further analysis and processing. Since WeChat does not provide a public API to obtain the content of official account articles, we need to use a web crawler to achieve this requirement.

Crawl process

We will use the Watir library to simulate browser behavior and achieve access and content acquisition to the specified WeChat official account page. The Watir library is a simple and powerful Ruby library that can simulate user operations in the browser, including clicking links, filling out forms, etc.

Anti-climbing strategy

When performing web crawling, we need to consider the anti-crawling strategies that the target website may adopt. In order to circumvent the anti-crawler mechanism, we will use a proxy server to hide our real IP address and simulate the access behavior of human users, such as setting access intervals, random User-Agent, etc.

Capture idea analysis

1. First, we need to analyze the request of the WeChat official account page and understand the page structure and data loading method.
2. By analyzing the page request, we can find the data source of the WeChat public account article content, which may be JSON data obtained through the interface.
3. We need to analyze the rules of the interface and understand how to construct request parameters and obtain data.
4. By constructing the request parameters, we can use the Watir library to simulate the request interface and obtain the data of the WeChat public account article content.
5. The obtained data may need to be filtered and processed in order to extract the content we need and conduct further analysis.

Implement code

    require 'watir'
require 'open-uri'
require 'json'

# 设置代理服务器
proxyHost = "www.16yun.cn"
proxyPort = "5445"
proxyUser = "16QMSOML"
proxyPass = "280651"

# 设置代理
proxy = "http://#{proxyUser}:#{proxyPass}@#{proxyHost}:#{proxyPort}"
browser = Watir::Browser.new :chrome, :switches => ['--proxy-server=#{proxy}']

# 访问微信公众号页面
browser.goto 'https://mp.weixin.qq.com/s/xxxxxxxxxxxxx'

# 获取接口数据
response = open('https://api.weixin.qq.com/article_content_api?article_id=xxxxxx', :proxy_http_basic_authentication => [proxyUser, proxyPass]).read
data = JSON.parse(response)

# 提取文章内容
article_content = data['content']

# 输出文章内容
puts article_content

# 关闭浏览器
browser.close



![Ruby and Watir libraries crawl the content of specified WeChat public accounts](6b44e99974d17195ee2722671aabdc1f.png)