搜狗微信搜索提供两种类型的关键词搜索,一种是搜索公众号文章内容,另外一种是直接搜索微信公众号。经过微信公众号搜索能够获取公众号的基本信息及最近发布的10条文章,以前写过一篇《Python 抓取微信公众号文章》,今天来抓取一下微信公众号的帐号信息(→ 先看结果(2998条) ←)。html
首先经过首页进入,能够按照类别抓取,经过“查看更多”能够找出页面连接规则:git
import requests as req
import re
reTypes = r'id="pc_\d*" uigs="(pc_\d*)">([\s\S]*?)</a>'
Entry = "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复制代码
上面代码经过加载更多页面获取加载列表,进而从其中抓取微信公众号详情页面:github
reProfile = r'<li id[\s\S]*?<a href="([\s\S]*?)"'
allProfiles = re.findall(reOAProfile, getUTF8(categoryList))
for profile in allProfiles:
profilePage = req.get(profile)
if profilePage.status_code != 200:
continue复制代码
进入详情页面能够获取公众号的名称/ID/功能介绍/帐号主体/头像/二维码/最近10篇文章
等信息:redis
详情页面连接:
http://mp.weixin.qq.com/profile?src=3×tamp=1477208282&ver=1&signature=8rYJ4QV2w5FXSOy6vGn37sUdcSLa8uoyHv3Ft7CrhZhB4wO-bbWG94aUCNexyB7lqRNSazua-2MROwkV835ilg==
sql
访问详情页面时有可能须要验证码,自动识别验证码仍是颇有难度的,所以要作好爬虫的假装工做。flask
详情页面的连接中有两个重要参数:timestamp & signature
,这说明页面连接是有时效性的,因此保存下来应该也没用;微信
二维码图片连接一样具备时效性,所以如须要最好将图片下载下来。app
最近 Python 社区出现了一款异步加强版的 Flask 框架:Sanic,基于uvloop和httptools,能够达到异步、更快的效果,但保持了与 Flask 一致的简洁语法。虽然项目刚起步,还有不少基本功能为实现,但已经得到了不少关注(2,222 Star)。此次本打算用抓取的微信公众号信息基于 Sanic 作一个简单的交互应用,但无奈目前尚未加入模板功能,异步的 redis 驱动也还有 BUG 没解决,因此简单尝试了一下以后仍是切换回 Flask + SQLite,先把抓取结果呈现出来,后续有机会再作更新。框架
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)复制代码
【查看原文】异步