脚本专栏 
首页 > 脚本专栏 > 浏览文章

Python 抓取微信公众号账号信息的方法

(编辑:jimmy 日期: 2025/5/8 浏览:3 次 )

Python 抓取微信公众号账号信息的方法

搜狗微信搜索提供两种类型的关键词搜索,一种是搜索公众号文章内容,另一种是直接搜索微信公众号。通过微信公众号搜索可以获取公众号的基本信息及最近发布的10条文章,今天来抓取一下微信公众号的账号信息

爬虫

首先通过首页进入,可以按照类别抓取,通过“查看更多”可以找出页面链接规则:

Python 抓取微信公众号账号信息的方法

import requests as req
import re
reTypes = r'id="pc_\d*" uigs="(pc_\d*)">([\s\S]*"http://weixin.sogou.com/"
entryPage = req.get(Entry)
allTypes = re.findall(reTypes, getUTF8(entryPage))
for (pcid, category) in allTypes:
  for page in range(1, 100):
    url = 'http://weixin.sogou.com/pcindex/pc/{}/{}.html'.format(pcid, page)
    print(url)
    categoryList = req.get(url)
    if categoryList.status_code != 200:
      break

上面代码通过加载更多页面获取加载列表,进而从其中抓取微信公众号详情页面:

reProfile = r'<li id[\s\S]*"([\s\S]*" rel="external nofollow" '
allProfiles = re.findall(reOAProfile, getUTF8(categoryList))
for profile in allProfiles:
  profilePage = req.get(profile)
  if profilePage.status_code != 200:
    continue

进入详情页面可以获取公众号的 名称/ID/功能介绍/账号主体/头像/二维码/最近10篇文章 等信息:

Python 抓取微信公众号账号信息的方法 

注意事项

详情页面链接: http://mp.weixin.qq.com/profile"text-align: center">Python 抓取微信公众号账号信息的方法

Debug Sanic

Python 抓取微信公众号账号信息的方法 

Flask + SQLite App

from flask import g, Flask, render_template
import sqlite3
app = Flask(__name__)
DATABASE = "./db/wx.db"
def get_db():
  db = getattr(g, '_database', None)
  if db is None:
    db = g._database = sqlite3.connect(DATABASE)
  return db
@app.teardown_appcontext
def close_connection(exception):
  db = getattr(g, '_database', None)
  if db is not None:
    db.close()
@app.route("/<int:page>")
@app.route("/")
def hello(page=0):
  cur = get_db().cursor()
  cur.execute("SELECT * FROM wxoa LIMIT 30 OFFSET ", (page*30, ))
  rows = []
  for row in cur.fetchall():
    rows.append(row)
  return render_template("app.html", wx=rows, cp=page)
if __name__ == "__main__":
  app.run(debug=True, port=8000)

总结

以上所述是小编给大家介绍的Python 抓取微信公众号账号信息,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

上一篇:对python tkinter窗口弹出置顶的方法详解
下一篇:python字典一键多值实例代码分享
一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 网站地图 SiteMap