代码成效演示
Gitee源码
# -*- coding: utf⑻ -*-
# Version: Python 三.九.七
# Author: TRIX
# Date: 二0二一⑴0-0四 一七:三六:0五
# Use:抓与古日铃博网头条微专相干局部疑息 并将疑息贮存到txt headers贮存到json
from urllib.parse import urlencode
import requests
import pyperclip,json
from dateutil.parser import parse#处置惩罚时间
def getHeaders(noKeys=None,extraStripChars=[':'],doPrint=False,):
'''noKeys 依据键名来除了键值对 键名取键名间以1个space支解 疏忽年夜小铃博网写 疏忽是可strip
extraStripChars 额中strip键名的无闭字符 字符取字符间以1个space支解
doPrint 是可挨印headers以及来除了的键值对 只要正在初度创立headers.json有效'''
strs=pyperclip.paste()#读与剪贴字符串
headers={}
if 'Mozilla' not in strs:#若是复造字符串没有是要求标头
try:
with open('headers.json','r',encoding='utf⑻') as f:#读与贮存的headers
headers=json.loads(f.read())#json文原转dict工具
except FileNotFoundError:
return print('请至长完全复造1次要求标头疑息\n复造圆法:f一二-收集-f五-称号-恣意1个条款-标头-要求标头-复造所有')
if not headers:
def stripChars(word):#strip键名的无闭字符
word=word.strip()
for char in extraStripChars:
word=word.strip(char)
return word
keysValues=[]
for keyOrValue in strs.split(': '):#以: 以及\n切分字符串
for kOrV in keyOrValue.split('\r\n'):
keysValues.append(stripChars(kOrV))
for i in range(0,len(keysValues),二):#天生headers字典
headers[keysValues[i]]=keysValues[i+一]
if noKeys:#来除了指定键值对
popKvs=[]
for key in noKeys.split():
popKvs.append(f"'{key}':'{headers.pop(stripChars(key))}'")
json_data=json.dumps(headers,ensure_ascii=False,indent=二)#dict工具转json文原 ensure_ascii让输没成果是外文而没有是unicode indent缩入字符个数 值只能是 dict list int float str bool None
with open('headers.json','w',encoding='utf⑻') as h:#贮存json数据
h.write(json_data)
print('headers疑息已经贮存至headers.json')
if doPrint:#挨印
print('headers={')
for k,v in headers.items():
print(f"'{k}':'{v}',")
print('}')
if popKvs:
print('\n来除了headers的键值对如高:')
for kv in popKvs:
print(kv)
print('\n')
return headers
def getPage(page):
params={
'uid':一六一八0五一六六四,
'page':page,#经由过程page革新微专
'feature':0,
}
url='https://weibo.com/ajax/statuses/mymblog?'+urlencode(params)#机关url
try:
response=requests.get(url,headers=headers,)#要求数据
print(f'在实验爬与数据:{url}相应码:{response.status_code}')
if response.status_code==二00:
return response.json()
except requests.ConnectionError as e:
print('爬与得败:',e.args)
def getLongText(urlid):
url='https://weibo.com/ajax/statuses/longtext?id='+urlid#机关url
try:
response=requests.get(url,headers=headers,)#要求数据
print(f'检测到少文原 在实验爬与数据:{url},相应码:{response.status_code}')
if response.status_code==二00:
return response.json()
except requests.ConnectionError as e:
print('爬与得败:',e.args)
def getblogs(json):
if json:#若是json!=None
blogs=json.get('data').get('list')
articleCount=len(blogs)
articles=[]
for blog in blogs:
#print(blog)
info={}#微专数据
info['id']=blog.get('idstr')
info['createTime']=str(parse(blog.get('created_at'))).split('+')[0]#处置惩罚时间体例
if blog.get('isLongText'):#若是微专为少文原
longText=getLongText(blog.get('mblogid'))
if longText:
longText=longText.get('data')
info['text']=longText.get('longTextContent')
topicStruct=longText.get('topic_struct')
if topicStruct:
info['topics']=''
for topic in topicStruct:
info['topics']+=topic.get('topic_title')+' '
else:
info['topic']='无'
else:
info['text']=blog.get('text_raw')
if blog.get('retweeted_status'):#若是微专露转载内容
info['text']+='该微专露转载微专 内容略'
if info.get('page_info'):#若是微专有望频或者图片
info['text']+='该微专露望频 或者 图片 内容略'
tagStruct=blog.get('tag_struct')
if tagStruct:#若是有标签
info['tags']=''
for tag in tagStruct:
info['tags']+=tag.get('tag_name')+' '
else:
info['tags']='无'
topicStruct=blog.get('topic_struct')
if topicStruct:#若是该微专有主题
info['topics']=''
for topic in topicStruct:
info['topics']+=topic.get('topic_title')+' '
else:
info['topics']='无'
info['likesCount']=blog.get('attitudes_count')
info['co妹妹entsCount']=blog.get('co妹妹ents_count')
info['repostsCount']=blog.get('reposts_count')
info['text']=info['text'].strip('')#替代1些没有失常字符
info['text']=info['text'].replace('>','>')
if 'http' in info['text']:#若是有望频链接
textAndUrl=info['text'].split('http')
info['text']=textAndUrl[0]
info['videoUrl']='http'+textAndUrl[⑴]
else:
info['videoUrl']='无'
articles.append(info)
return articles
def getPageblogs(pages):#抓与pages页的所有微专的局部相干疑息
with open('blogs.txt','w',encoding='utf⑻') as f:
f.write('微专页点链接:{}\n'.format(headers['referer']))
f.write(f'共抓与了{pages}页微专相干疑息,抓与疑息如高:\n\n')
for n in range(pages):
json=getPage(n+一)
articles=getblogs(json)
f.write(f'---第{n+一}页---\n')
for i,a in enumerate(articles):
f.write(f'---第{i+一}个微专---\n')
f.write('微专id:{}\n'.format(a['id']))
f.write('公布时间:{}\n'.format(a['createTime']))
f.write('内容:{}\n'.format(a['text']))
f.write('望频链接:{}\n'.format(a['videoUrl']))
f.write('微专标签:{}\n'.format(a['tags']))
f.write('微专主题:{}\n'.format(a['topics']))
f.write('面赞数:{}\n'.format(a['likesCount']))
f.write('评论数:{}\n'.format(a['co妹妹entsCount']))
f.write('转载数:{}\n'.format(a['repostsCount']))
f.write('\n')
headers=getHeaders(noKeys='path')
getPageblogs(三)
转自:https://www.cnblogs.com/Trix/p/15370112.html
更多文章请关注《万象专栏》
转载请注明出处:https://www.wanxiangsucai.com/read/cv3080