跳到导航

公众号文章标的省份和国家,能用来筛本地内容吗?

可以,作为筛选线索使用。wechat_mp.v2.fetch_article_detail_h5 文章详情里的 province_name 和 country_name 描述文章在哪里发布,不描述文章主题所在地,也不描述读者在哪。要整理“上海发布的文章”,按已保存详情里返回的省份筛;要找“讨论上海的文章”,还要看每篇文章的标题和正文。

“上海发布的文章”和“讨论上海的文章”是两份不同的清单。例如,一篇在北京发布的上海招聘文章,应放进“上海岗位信息”,但不属于“上海发文样本”。筛选前先确定要回答哪一种地域问题。

按你的目的选依据

想知道什么看什么
这篇文章显示在哪个省份或国家发布详情返回的发文属地字段
文章讨论哪个城市、活动或岗位标题、正文及其上下文
这个账号的读者主要在哪有明确读者地域定义的数据;发文属地回答不了

同一账号的不同文章可能显示不同发文属地。整理时把每个属地标签和对应文章、原文链接、发布时间、查询时间放在同一行。

已经有一批文章,怎样筛?

  1. 复用正文读取流程保存的详情,从每条 data:wechat_mp.v2.fetch_article_detail_h5 结果读取 data.content.province_name 和 data.content.country_name。
  2. 把实际返回的属地标签和文章链接放在一行,另加你自己的主题判断,例如“上海岗位”或“全国行业分析”。
  3. 按目的筛选。做本地选题时先按主题相关性筛;研究发文分布时按属地标签分组。没有属地标签的文章单列一组。

wechat_mp.v2.fetch_article_detail_h5 按文章 URL 逐篇取数,所以地域筛选在取回详情之后进行。空值记为“未提供”。文章详情已经保存过时,直接从保存的详情提取属地字段,不必重新请求。

例如文章显示发文属地「北京」,正文却明确写「岗位在上海」。招聘清单把上海填在「岗位城市」,北京填在「发文属地」。筛可投递的上海岗位时按「岗位城市」筛;研究发布标签时按「发文属地」分组。

怎样读一份地域统计?

假设选了10篇文章,其中6篇返回上海、2篇返回北京、2篇未提供。可以写:“本次10篇样本中,8篇带发文省份,其中6篇为上海;2篇缺失。”如果计算有值样本占比,上海是6/8;如果按全部样本算,则是6/10,表头要写清分母。

这份地域统计描述的是选定文章的发文标签。它不提供读者人数,也不能由此得出“六成粉丝在上海”。

可选:把已保存的属地信息导出成表

建立 locations-input.json,每个完全相同的文章请求URL选择一次已存观测,填入请求链接、采集时间与响应文件:

[{"url":"https://mp.weixin.qq.com/s/REPLACE_WITH_REAL_ARTICLE","collected_at":"2026-11-16T10:00:00Z","file":"article-1/wechat-article.json"}]

把下面的 Python 脚本保存为 publication_locations.py,运行 python3 publication_locations.py locations-input.json location-review。publication_locations.py 支持 1–50 条观测,生成 publication-locations.csv、精确值 JSON、coverage.json 和原响应副本;输出目录已存在时脚本停止,不覆盖。

import collections,csv,hashlib,json,pathlib,sys
from datetime import datetime
from urllib.parse import parse_qs,urlsplit
manifest,out=map(pathlib.Path,sys.argv[1:3]);manifest_bytes=manifest.read_bytes();entries=json.loads(manifest_bytes)
if not isinstance(entries,list) or not 1<=len(entries)<=50:raise ValueError('Supply 1-50 saved observations')
def article_url(value):
if not isinstance(value,str):raise ValueError('Expected requested article URL')
u=urlsplit(value)
if u.scheme!='https' or u.hostname!='mp.weixin.qq.com' or u.username is not None or u.password is not None or u.port not in (None,443):raise ValueError('Expected HTTPS WeChat article')
if u.path=='/s':
q=parse_qs(u.query,keep_blank_values=True)
if not all(len(q.get(k,[]))==1 and q[k][0].strip() for k in ('__biz','mid','idx')):raise ValueError('Long URL lacks article identity')
if not q['mid'][0].isascii() or not q['mid'][0].isdigit() or not q['idx'][0].isascii() or not q['idx'][0].isdigit() or int(q['idx'][0])<1:raise ValueError('Bad article position')
elif not u.path.startswith('/s/') or not u.path[3:] or '/' in u.path[3:]:raise ValueError('Expected article path')
return value
def field(content,key):
if key not in content:return None,'absent'
value=content[key]
if value is None:return None,'null'
if not isinstance(value,str):raise ValueError('Location must be a string or missing')
return value,('reported' if value.strip() else 'empty')
rows=[];raws=[];seen_requests=set();seen_urls=set();countries=collections.Counter();provinces=collections.Counter()
for n,entry in enumerate(entries,1):
url=article_url(entry['url']);stamp=datetime.fromisoformat(entry['collected_at'].replace('Z','+00:00'))
if stamp.utcoffset() is None:raise ValueError('Collection time needs a timezone')
if url in seen_urls:raise ValueError('Choose one observation per exact requested URL')
seen_urls.add(url);path=manifest.parent/entry['file'];raw=path.read_bytes();r=json.loads(raw)
if not isinstance(r,dict) or r.get('status')!='succeeded' or r.get('action')!='wechat_mp.v2.fetch_article_detail_h5' or r.get('error') is not None:raise ValueError('Expected successful article detail')
rid=r.get('request_id');data=r.get('data')
if not isinstance(rid,str) or not rid.strip() or rid in seen_requests or not isinstance(data,dict) or not isinstance(data.get('content'),dict):raise ValueError('Missing/repeated provenance or content')
seen_requests.add(rid)
if data.get('url') is not None and data['url']!=url:raise ValueError('Response request URL differs')
content=data['content'];country,cs=field(content,'country_name');province,ps=field(content,'province_name')
if cs=='reported':countries[country]+=1
if ps=='reported':provinces[(country if cs=='reported' else None,province)]+=1
row={'requested_url':url,'collected_at':entry['collected_at'],'country_name':country,'country_state':cs,'province_name':province,'province_state':ps,'request_id':rid,'request_url_echo_verified':data.get('url')==url,'source_sha256':hashlib.sha256(raw).hexdigest(),'source_file':str(path.resolve()),'archive_file':'raw-%02d.json'%n}
rows.append(row);raws.append(raw)
def safe(v):
if v is None:return ''
s=str(v)
return "'"+s if s.lstrip().startswith(('=','+','-','@')) or s.startswith(('\t','\r','\n')) else s
report={'saved_observations':len(rows),'unique_request_urls':len(seen_urls),'url_aliases_resolved':False,'country_states':dict(collections.Counter(x['country_state'] for x in rows)),'province_states':dict(collections.Counter(x['province_state'] for x in rows)),'countries':[{'country_name':k,'observations':v} for k,v in sorted(countries.items())],'province_groups':[{'country_name':k[0],'province_name':k[1],'observations':v} for k,v in sorted(provinces.items(),key=lambda item:json.dumps(item[0],ensure_ascii=False))],'manifest_sha256':hashlib.sha256(manifest_bytes).hexdigest(),'scope':'Selected saved article observations; not readers, unique authors, account addresses or audience geography'}
out.mkdir();(out/'manifest.json').write_bytes(manifest_bytes)
for row,raw in zip(rows,raws):(out/row['archive_file']).write_bytes(raw)
with (out/'publication-locations.csv').open('x',encoding='utf-8-sig',newline='') as f:
w=csv.DictWriter(f,fieldnames=list(rows[0]));w.writeheader();w.writerows({k:safe(v) for k,v in row.items()} for row in rows)
(out/'publication-locations.json').write_text(json.dumps(rows,ensure_ascii=False,indent=2),encoding='utf-8')
(out/'coverage.json').write_text(json.dumps(report,ensure_ascii=False,indent=2),encoding='utf-8')
print(str(out))

在 CSV 里筛选查看,在 JSON 里读原值;缺字段、null、空字符串和明确返回的属地分别记录。脚本按 saved_observations 计数,不会把同一篇文章的短链接和长链接合并,所以选文时自己核对长短 URL 别名。国家缺失时,脚本不会从省份反推国家。

新取文章详情的价格见当前价格页。本地提取和统计复用已保存的结果,不产生新请求。

公众号问题目录