微信公众号爬虫

简介: 微信团队于2017-06-06发布更新:“ 对所有公众号开放,在图文消息正文中插入自己帐号和其他公众号已群发文章链接的能力。”那么,利用这个接口,我们就可以爬取指定公众号的文章链接了文章参考:静觅准备工具:一个订阅号,安装selenium爬取步骤:get_cookie.

微信团队于2017-06-06发布更新:

“ 对所有公众号开放,在图文消息正文中插入自己帐号和其他公众号已群发文章链接的能力。”

那么,利用这个接口,我们就可以爬取指定公众号的文章链接了

文章参考:静觅

准备工具:一个订阅号,安装selenium

爬取步骤:


img_8be5a67532fab7451870bd639377fa40.jpe

img_b271fc3b93e503b622595f05bf6ff4a0.jpe

img_2d06a09bec0240080cdb4fd9acb3ddf0.jpe
  1. get_cookie.py用selenium登陆,获取cookie,其中你需要勾选“记住”选项,还需要微信扫描二维码,确定顺利登陆
# -*- coding:utf-8 -*-
from selenium import webdriver
import io
import time
import json
from pprint import pprint

post = {}

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36")
driver = webdriver.Chrome('/Users/weiweiwang/code/financialNews/chromedriver',chrome_options=chrome_options)
driver.get('https://mp.weixin.qq.com/')
time.sleep(2)
driver.find_element_by_xpath("./*//input[@name='account']").clear()
driver.find_element_by_xpath("./*//input[@name='account']").send_keys('你的账号')
driver.find_element_by_xpath("./*//input[@name='password']").clear()
driver.find_element_by_xpath("./*//input[@name='password']").send_keys('你的密码')
# 在自动输完密码之后记得点一下记住我
time.sleep(5)
driver.find_element_by_xpath("./*//a[@class='btn_login']").click()
# 拿手机扫二维码!
time.sleep(15)
driver.get('https://mp.weixin.qq.com/')
cookie_items = driver.get_cookies()
for cookie_item in cookie_items:
    post[cookie_item['name']] = cookie_item['value']
cookie_str = json.dumps(post)
with io.open('cookie.txt', 'w+') as f:
    f.write(cookie_str.decode('utf-8'))
  1. get_url.py获取你需要的公众号的历史文章链接
# -*- coding:utf-8 -*-
import requests
import io
import redis
import json
import re
import random
import time

gzlist = ['yq_Butler']


url = 'https://mp.weixin.qq.com'
header = {
    "HOST": "mp.weixin.qq.com",
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"
    }

with io.open('cookie.txt', 'r') as f:
    cookie = f.read()
cookies = json.loads(cookie)
response = requests.get(url=url, cookies=cookies)
token = re.findall(r'token=(\d+)', str(response.url))[0]
for query in gzlist:
    query_id = {
        'action': 'search_biz',
        'token' : token,
        'lang': 'zh_CN',
        'f': 'json',
        'ajax': '1',
        'random': random.random(),
        'query': query,
        'begin': '0',
        'count': '5',
    }
    search_url = 'https://mp.weixin.qq.com/cgi-bin/searchbiz?'
    search_response = requests.get(search_url, cookies=cookies, headers=header, params=query_id)
    lists = search_response.json().get('list')[0]
    fakeid = lists.get('fakeid')
    query_id_data = {
        'token': token,
        'lang': 'zh_CN',
        'f': 'json',
        'ajax': '1',
        'random': random.random(),
        'action': 'list_ex',
        'begin': '0',
        'count': '5',
        'query': '',
        'fakeid': fakeid,
        'type': '9'
    }
    appmsg_url = 'https://mp.weixin.qq.com/cgi-bin/appmsg?'
    appmsg_response = requests.get(appmsg_url, cookies=cookies, headers=header, params=query_id_data)
    max_num = appmsg_response.json().get('app_msg_cnt')
    num = int(int(max_num) / 5)
    begin = 0
    while num + 1 > 0 :
        query_id_data = {
            'token': token,
            'lang': 'zh_CN',
            'f': 'json',
            'ajax': '1',
            'random': random.random(),
            'action': 'list_ex',
            'begin': '{}'.format(str(begin)),
            'count': '5',
            'query': '',
            'fakeid': fakeid,
            'type': '9'
        }
        print('翻页###################',begin)
        query_fakeid_response = requests.get(appmsg_url, cookies=cookies, headers=header, params=query_id_data)
        fakeid_list = query_fakeid_response.json().get('app_msg_list')
        for item in fakeid_list:
            print(item.get('link'))
        num -= 1
        begin = int(begin)
        begin+=5
        time.sleep(2)

所有代码见github

目录
相关文章
|
4月前
|
数据采集 存储 数据挖掘
Python爬虫实战:打造一个简单的新闻网站数据爬取工具
本文将介绍如何运用Python编写一个简单而高效的网络爬虫,帮助您在实际项目中快速获取并存储新闻网站的数据。通过学习本文,您将了解到如何利用Python中的第三方库和技术来实现数据爬取,为您的数据分析和应用提供更多可能性。
|
4月前
|
数据采集 存储 关系型数据库
Python爬虫-使用代理获取微信公众号文章
使用代理爬取微信公众号文章
193 0
|
XML 数据采集 Web App开发
Python爬虫篇:爬虫笔记合集
Python爬虫篇:爬虫笔记合集
115 0
|
数据采集 Python
python爬虫抓取富贵论坛
本人是个爬虫小萌新,看了网上教程学着做了一些,如果有什么问题请大佬们反馈,谢谢。
264 0
python爬虫抓取富贵论坛
|
Web App开发 数据采集 前端开发
|
数据采集 JSON 数据格式
Python爬虫入门教程 35-100 知乎网全站用户爬虫 scrapy
爬前叨叨 全站爬虫有时候做起来其实比较容易,因为规则相对容易建立起来,只需要做好反爬就可以了,今天咱们爬取知乎。继续使用scrapy当然对于这个小需求来说,使用scrapy确实用了牛刀,不过毕竟本博客这个系列到这个阶段需要不断使用scrapy进行过度,so,我写了一会就写完了。
1367 0
|
数据采集 存储 Python
Python爬虫入门教程 34-100 掘金网全站用户爬虫 scrapy
爬前叨叨 已经编写了33篇爬虫文章了,如果你按着一个个的实现,你的爬虫技术已经入门,从今天开始慢慢的就要写一些有分析价值的数据了,今天我选了一个《掘金网》,我们去爬取一下他的全站用户数据。 爬取思路 获取全站用户,理论来说从1个用户作为切入点就可以,我们需要爬取用户的关注列表,从关注列表不断的叠加下去。
1213 0
|
数据采集 API Python
Python爬虫入门教程 25-100 知乎文章图片爬取器之一
1. 知乎文章图片爬取器之一写在前面 今天开始尝试爬取一下知乎,看一下这个网站都有什么好玩的内容可以爬取到,可能断断续续会写几篇文章,今天首先爬取最简单的,单一文章的所有回答,爬取这个没有什么难度。
1472 0
|
数据采集 Web App开发 iOS开发
python爬虫系列之初识爬虫
python爬虫系列之初识爬虫前言 我们这里主要是利用requests模块和bs4模块进行简单的爬虫的讲解,让大家可以对爬虫有了初步的认识,我们通过爬几个简单网站,让大家循序渐进的掌握爬虫的基础知识,做网络爬虫还是需要基本的前端的知识的,下面我们进行我们的爬虫讲解 在进行实战之前,我们先给大家看下...
1548 0
|
数据采集 数据库 Python
基于Scrapy爬取伯乐在线网站(进阶版)
标题中的英文首字母大写比较规范,但在python实际使用中均为小写。 爬取伯乐在线网站所有文章的详情页面 1.网页持久化 1.1 新建爬虫工程 新建爬虫工程命令:scrapy startproject BoleSave2 image.png 进入爬虫工程目录命令:cd BoleSave2 新建爬虫文件命令:scrapy genspider save blog.jobbole.com 1.2 编辑save.py文件 网页持久化只需要编辑爬虫文件就可以,下面是save.py文件的代码。
1047 0