# 生成随机数 import random # 模仿浏览器的requests import urllib.request # 字符串转换 import json # 政策表达式库 import re # 请求库 import requests # 时间库 import time # 输入目标微博数字id id=(input("请输入要抓的微博uid:")) # 名字初始化 na='a' # IP代理池 iplist=['112.228.161.57:8118','125.126.164.21:34592','122.72.18.35:80','163.125.151.124:9999','114.250.25.19:80'] # 代理服务器,包含上面IP池 proxy_addr="163.125.151.124:9999" # 用代理服务器伪装自己 def use_proxy(url,proxy_addr): # 访问目标网址,借用urllib.requests.Requests伪装 req=urllib.request.Request(url) # 头部信息 req.add_header("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0") # 代理服务器设置,后为随机选择IP池中的代理IP proxy=urllib.request.ProxyHandler({'http':random.choice(iplist)}) # 解决代理代理服务器的cookie等问题,参数前为代理服务器,后为处理 opener=urllib.request.build_opener(proxy,urllib.request.HTTPHandler) # 安装代理服务器 urllib.request.install_opener(opener) # 代理服务器的编码赋值 data=urllib.request.urlopen(req).read().decode('utf-8','ignore') # 返回,至此代理服务器的设置完成 return data def get_containerid(url): # 使用代理服务器 参数分别为 网址,随机选择IP url,random,choice(iplist) data=use_proxy(url,random.choice(iplist)) # 字符格式转换json化 # json.loads(data).get('data')其中get为get其中的json格式的data content=json.loads(data).get('data') # 解析网址找到图片文件所在处 for data in content.get('tabsInfo').get('tabs'): if(data.get('tab_type')=='weibo'): containerid=data.get('containerid') return containerid # 获取微博目标基本信息 def get_userInfo(id): # 微博目标用户头像Photo url='https://m.weibo.cn/api/container/getIndex?type=uid&value='+id # 再次使用代理服务器 data=use_proxy(url,random.choice(iplist)) # 再次json化数据并赋值给content content=json.loads(data).get('data') # 头像 profile_image_url=content.get('userInfo').get('profile_image_url') description=content.get('userInfo').get('description') profile_url=content.get('userInfo').get('profile_url') verified=content.get('userInfo').get('verified') guanzhu=content.get('userInfo').get('follow_count') name=content.get('userInfo').get('screen_name') na=name fensi=content.get('userInfo').get('followers_count') gender=content.get('userInfo').get('gender') urank=content.get('userInfo').get('urank') if (gender=="f"): gender="女" else: gender="男" print("微博昵称:"+name+"\n"+"微博主页地址:"+profile_url+"\n"+"微博头像地址:"+profile_image_url+"\n"+"是否认证:"+str(verified)+"\n"+"微博说明:"+description+"\n"+"关注人数:"+str(guanzhu)+"\n"+"粉丝数:"+str(fensi)+"\n"+"性别:"+gender+"\n"+"微博等级:"+str(urank)+"\n") def get_weibo(id,file): # 初始化循环值,i为页 i=1 # 定义保存目录 Directory = './111programmer/' # while true 为循环核心逻辑 while True: # 目标对象的URL url='https://m.weibo.cn/api/container/getIndex?type=uid&value='+id # 目标对象相册 weibo_url='https://m.weibo.cn/api/container/getIndex?type=uid&value='+id+'&containerid='+get_containerid(url)+'&page='+str(i) try: data=use_proxy(weibo_url,random.choice(iplist)) content=json.loads(data).get('data') cards=content.get('cards') if(len(cards)>0): # range 范围,在cards长度范围内进行搜索 for j in range(len(cards)): # i为页,j为条 print("-----正在爬取第"+str(i)+"页,第"+str(j)+"条微博------") card_type=cards[j].get('card_type') # 9为转发微博 目前不知道 if(card_type==9): mblog=cards[j].get('mblog') # 微博card # print(mblog) # 找到转发微博tag #print(str(mblog).find("转发微博")) # 根据转发量判断热门微博?个人认为没有用,注释掉 # if str(mblog).find('retweeted_status') == -1: # 因取消上一个if判断 所以这里操作了缩进 # -1为异常状态,意为图片不存在 if str(mblog).find('original_pic') !=-1: # re.findall(正则)找到并通过正则找到符合条件的图片 img_url=re.findall(r"'url': '(.+?)'", str(mblog))##pics(.+?) # 图片数字的count n = 1 # 时间的处理,str字符串化 timename = str(time.time()) # 后代替前word timename = timename.replace('.', '') # 表示最后一个到第七个,也可以说是第七个到最后一个 timename = timename[7:]#利用时间作为独特的名称 # 循环目标图片网址 for url in img_url: print('第' + str(n) + ' 张', end='') # with多少有点强制执行的概念,如果with后的open执行失败,那么就会使用as后的写入 # wb为二进制方式打开 with open(Directory + timename+url[-5:], 'wb') as f: f.write(requests.get(url).content) print('...OK!') n = n + 1 # 不是特别精明的延迟算法 if( n%3==0 ): ##延迟爬取,防止截流 time.sleep(3) # 点赞数 attitudes_count=mblog.get('attitudes_count') # 评论数 comments_count=mblog.get('comments_count') # 发布此条微博时间 created_at=mblog.get('created_at') # 转发数 reposts_count=mblog.get('reposts_count') # 此条微博地址 scheme=cards[j].get('scheme') # 此条微博内容 text=mblog.get('text') # 存入TXT文件 with open(file,'a',encoding='utf-8') as fh: fh.write("----第"+str(i)+"页,第"+str(j)+"条微博----"+"\n") fh.wrnameite("微博地址:"+str(scheme)+"\n"+"发布时间:"+str(created_at)+"\n"+"微博内容:"+text+"\n"+"点赞数:"+str(attitudes_count)+"\n"+"评论数:"+str(comments_count)+"\n"+"转发数:"+str(reposts_count)+"\n") i+=1 else: break # 报出错误并pass跳过 except Exception as e: print(e) pass if __name__=="__main__": file='./111/'+id+".txt" get_userInfo(id) get_weibo(id,file)
Python微博图
最新推荐文章于 2024-07-27 12:20:46 发布
# 生成随机数import random# 模仿浏览器的requestsimport urllib.request# 字符串转换import json# 政策表达式库import re# 请求库import requests# 时间库import time# 输入目标微博数字idid=(input("请输入要抓的微博uid:"))# 名字初始化na='a'# IP代理池iplist=['112.228.161.57:8118','125.126.164.21:34592.
摘要由CSDN通过智能技术生成