本人想搞個采集微信文章的網站,無奈實在從微信本生無法找到入口鏈接,網上翻看了大量的資料,發現大家的做法總體來說大同小異,都是以搜狗為入口。下文是筆者整理的一份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)
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
