python爬虫,微博爬虫,需要知晓微博用户id号,能够通过抓取微博用户主页内容来获取用户发表的内容,时间,点赞数,转发数等数据,当然以上都是本渣渣结合网上代码抄抄改改获取的!

要抓取的微博地址:https://weibo.com/u/5118612601

BUT,我们实际应用的抓取地址:https://m.weibo.cn/u/5118612601(移动端的微博地址)

LSP的最爱,各种小姐姐,随你任意爬取,快收藏起来啊!

通过浏览器抓包,我们可以获悉几个比较重要的参数:

  1. type: uid
  2. value: 5118612601
  3. containerid: 1005055118612601

其实还有一个比较重要的参数,那就是翻页:'page':page!

还有一个SSL错误问题,大家可以自行处理!

  1. import logging
  2. logging.captureWarnings(True)
  3.  
  4. # 屏蔽warning信息
  5. requests.packages.urllib3.disable_warnings()
  6.  
  7. html=requests.get(self.url,headers=self.headers,params=params,timeout=5,verify=False).content.decode('utf-8')
  8.  

几个关键点

  • 获取 containerid 参数

  1.     def get_containerid(self):
  2.         url = f'https://m.weibo.cn/api/container/getIndex?type=uid&value={self.uid}'
  3.         data = requests.get(url,headers=self.headers,timeout=5,verify=False).content.decode('utf-8')
  4.         content = json.loads(data).get('data')
  5.         for data in content.get('tabsInfo').get('tabs'):
  6.             if (data.get('tab_type') == 'weibo'):
  7.                 containerid = data.get('containerid')
  8.  
  9.         self.containerid=containerid
  • 获取 微博用户发表 数据

  1.     def get_content(self,i):
  2.         params={
  3.             'type': 'uid',
  4.             'value': self.uid,
  5.             'containerid': self.containerid,
  6.             'page':i,
  7.         }
  8.         html=requests.get(self.url,headers=self.headers,params=params,timeout=5,verify=False).content.decode('utf-8')
  9.         data=json.loads(html)['data']
  10.         cards=data['cards']
  11.         #print(cards)
  12.         j = 1
  13.         for card in cards:
  14.             if "mblog" in str(card):
  15.                 mblog = card['mblog']
  16.                 raw_text = mblog['raw_text']  # 文本内容
  17.                 print(raw_text)
  18.                 scheme=card['scheme'] #微博链接
  19.                 attitudes_count = mblog.get('attitudes_count') #点赞数
  20.                 comments_count = mblog.get('comments_count') #评论数
  21.                 created_at = mblog.get('created_at') #发布时间
  22.                 reposts_count = mblog.get('reposts_count') #转发数
  23.                 print(scheme)
  24.                 img_path=f'{self.path}{i}/{j}'
  25.                 os.makedirs(f'{img_path}/',exist_ok=True)
  26.                 with open(f'{img_path}/{j}.txt', 'a', encoding='utf-8') as f:
  27.                     f.write(f'{raw_text}')
  28.  
  29.                 img_urls=[]
  30.                 if mblog.get('pics') != None:
  31.                     img_datas=mblog['pics']
  32.                     for img_data in img_datas:
  33.                         img_url=img_data['large']['url']
  34.                         img_urls.append(img_url)
  35.                     print(img_urls)
  36.  
  37.                     #多线程下载图片
  38.                     self.get_imgs(img_urls,img_path)
  39.  
  40.                     #多进程下载图片
  41.                     #self.get_pimgs(img_urls)
  42.  
  43.                 with open(f'{self.uid}/{self.uid}.txt', 'a', encoding='utf-8') as fh:
  44.                     fh.write("----第" + str(i) + "页,第" + str(j) + "条微博----" + "\n")
  45.                     fh.write(f"微博地址: {str(scheme)}\n微博内容:{raw_text}\n"
  46.                              f"发布时间:{str(created_at)}\n转发数:{str(reposts_count)}\n"
  47.                              f"点赞数:{str(attitudes_count)}\n评论数:{str(comments_count)}\n\n")
  48.                 j=j+1
  49.  
  50.                 time.sleep(2)
  51.  
  • 多线程下载图片

  1.     #多线程下载图片
  2.     def get_imgs(self,img_urls,img_path):
  3.         threadings = []
  4.         for img_url in img_urls:
  5.             t = threading.Thread(target=self.get_img, args=(img_url,img_path))
  6.             threadings.append(t)
  7.             t.start()
  8.  
  9.         for x in threadings:
  10.             x.join()
  11.  
  12.         print("多线程下载图片完成")
  13.  
  14.  
  15.     def get_img(self, img_url,img_path):
  16.         img_name = img_url.split('/')[-1]
  17.         print(f'>> 正在下载图片:{img_name} ..')
  18.         r = requests.get(img_url, timeout=8, headers=self.headers,verify=False)
  19.         with open(f'{img_path}/{img_name}', 'wb') as f:
  20.             f.write(r.content)
  21.         print(f'>> 图片:{img_name} 下载完成!')
  22.  

本来还想搞个多进程,结果翻车了,报错各种头秃,那就不搞了!!

本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理

想要获取更多Python学习资料可以加QQ:2955637827私聊或加Q群630390733大家一起来学习讨论吧!

最新文章

  1. [SharePoint 2010] Copy list item with version history and attachment
  2. jquery.color.js的使用
  3. POJ - 1666 Candy Sharing Game
  4. div滚动条演示
  5. Apple Catching(dp)
  6. 适配器模式及C++实现
  7. dubbo 笔记-XML配置文件简介
  8. 从Trie树到双数组Trie树
  9. React Native之本地文件系统访问组件react-native-fs的介绍与使用
  10. pygame-KidsCanCode系列jumpy-part17-mask-collide碰撞检测
  11. animation特效
  12. Java - 获取帮助信息
  13. windows10 自带笔记本键盘禁止和开启
  14. centos cgroup配置
  15. ASP.NET MVC5+EF6+EasyUI 后台管理系统(1)-前言与目录(转)
  16. 如何分析python的性能(linux)
  17. Intervals ZOJ - 3953 (区间贪心)
  18. 错误 6 未能找到类型或命名空间名称“BLL”(是否缺少 using 指令或程序集引用?)
  19. 19、Flask实战第19天:CSRF攻击与防御
  20. Spring 事务机制详解(事务的隔离性和传播性)

热门文章

  1. jQuery 第四章 实例方法 DOM操作_基于jQuery对象增删改查相关方法
  2. Jmeter-记一次AES加密登录实例
  3. CodeBlocks相关配置
  4. vscode 中 eslint prettier 和 eslint -loader 配置关系
  5. sqli-labs-master less01
  6. 关于 [栈溢出后jmp esp执行shellcode] 原理分析
  7. SpringBoot中的Tomcat是如何启动的?
  8. C#数据结构-二叉树-链式存储结构
  9. 第7.10节 Python类中的实例变量定义与使用
  10. Python中排序方法sort、函数sorted的key参数的作用分析