python 爬取微信文章

yipeiwu_com5年前Python爬虫

本人想搞个采集微信文章的网站,无奈实在从微信本生无法找到入口链接,网上翻看了大量的资料,发现大家的做法总体来说大同小异,都是以搜狗为入口。下文是笔者整理的一份python爬取微信文章的代码,有兴趣的欢迎阅读

#coding:utf-8
author = 'haoning'
**#!/usr/bin/env python
import time
import datetime
import requests**
import json
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
import re
import xml.etree.ElementTree as ET
import os
#OPENID = 'oIWsFtyel13ZMva1qltQ3pfejlwU'
OPENID = 'oIWsFtw_-W2DaHwRz1oGWzL-wF9M&ext'
XML_LIST = []
# get current time in milliseconds
current_milli_time = lambda: int(round(time.time() * 1000))
def get_json(pageIndex):

global OPENID
the_headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36',
'Referer': 'http://weixin.sogou.com/gzh?openid={0}'.format(OPENID),
'Host': 'weixin.sogou.com'
}

url = 'http://weixin.sogou.com/gzhjs?cb=sogou.weixin.gzhcb&openid={0}&page={1}&t={2}'.format(OPENID, pageIndex, current_milli_time()) #url
print(url)

response = requests.get(url, headers = the_headers)
# TO-DO; check if match the reg
response_text = response.text
print response_text
json_start = response_text.index('sogou.weixin.gzhcb(') + 19
json_end = response_text.index(')') - 2
json_str = response_text[json_start : json_end] #get json
#print(json_str)
# convert json_str to json object
json_obj = json.loads(json_str) #get json obj
# print json_obj['totalPages']
return json_obj
def add_xml(jsonObj):

global XML_LIST
xmls = jsonObj['items'] #get item
#print type(xmls)
XML_LIST.extend(xmls) #用新列表扩展原来的列表
**[#www.oksousou.com][2]**
# ------------ Main ----------------
print 'play it :) '
# get total pages
default_json_obj = get_json(1)
total_pages = 0
total_items = 0
if(default_json_obj):

# add the default xmls
add_xml(default_json_obj)
# get the rest items
total_pages = default_json_obj['totalPages']
total_items = default_json_obj['totalItems']
print total_pages
# iterate all pages
if(total_pages >= 2):
  for pageIndex in range(2, total_pages + 1):
    add_xml(get_json(pageIndex)) #extend
    print 'load page ' + str(pageIndex)
    print len(XML_LIST)

相关文章

python3写爬取B站视频弹幕功能

python3写爬取B站视频弹幕功能

需要准备的环境: 一个B站账号,需要先登录,否则不能查看历史弹幕记录 联网的电脑和顺手的浏览器,我用的Chrome Python3环境以及request模块,安装使用命令,换源比较快:...

python实现爬虫统计学校BBS男女比例(一)

python实现爬虫统计学校BBS男女比例(一)

一、项目需求 前言:BBS上每个id对应一个用户,他们注册时候会填写性别(男、女、保密三选一)。 经过检查,BBS注册用户的id对应1-300000,大概是30万的用户 笔者想用Pyth...

python爬虫爬取监控教务系统的思路详解

python爬虫爬取监控教务系统的思路详解

这几天考了大大小小几门课,教务系统又没有成绩通知功能,为了急切想知道自己挂了多少门,于是我写下这个脚本。 设计思路: 设计思路很简单,首先对已有的成绩进行处理,变为list集合,然后定时...

python实现爬取图书封面

本文实例为大家分享了python实现爬取图书封面的具体代码,供大家参考,具体内容如下 kongfuzi.py 利用更换代理ip,延迟提交数据,设置请求头破解网站的反爬虫机制 impo...

python爬虫实现教程转换成 PDF 电子书

python爬虫实现教程转换成 PDF 电子书

写爬虫似乎没有比用 Python 更合适了,Python 社区提供的爬虫工具多得让你眼花缭乱,各种拿来就可以直接用的 library 分分钟就可以写出一个爬虫出来,今天就琢磨着写一个爬虫...