Python爬虫爬取新浪微博内容示例【基于代理IP】


Posted in Python onAugust 03, 2018

本文实例讲述了Python爬虫爬取新浪微博内容。分享给大家供大家参考,具体如下:

用Python编写爬虫,爬取微博大V的微博内容,本文以女神的微博为例(爬新浪m站:https://m.weibo.cn/u/1259110474

一般做爬虫爬取网站,首选的都是m站,其次是wap站,最后考虑PC站。当然,这不是绝对的,有的时候PC站的信息最全,而你又恰好需要全部的信息,那么PC站是你的首选。一般m站都以m开头后接域名, 所以本文开搞的网址就是 m.weibo.cn。

前期准备

1.代理IP

网上有很多免费代理ip,如西刺免费代理IPhttp://www.xicidaili.com/,自己可找一个可以使用的进行测试;

2.抓包分析

通过抓包获取微博内容地址,这里不再细说,不明白的小伙伴可以自行百度查找相关资料,下面直接上完整的代码

完整代码:

# -*- coding: utf-8 -*-
import urllib.request
import json
#定义要爬取的微博大V的微博ID
id='1259110474'
#设置代理IP
proxy_addr="122.241.72.191:808"
#定义页面打开函数
def use_proxy(url,proxy_addr):
  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")
  proxy=urllib.request.ProxyHandler({'http':proxy_addr})
  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
#获取微博主页的containerid,爬取微博内容时需要此id
def get_containerid(url):
  data=use_proxy(url,proxy_addr)
  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
#获取微博大V账号的用户基本信息,如:微博昵称、微博地址、微博头像、关注人数、粉丝数、性别、等级等
def get_userInfo(id):
  url='https://m.weibo.cn/api/container/getIndex?type=uid&value='+id
  data=use_proxy(url,proxy_addr)
  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')
  fensi=content.get('userInfo').get('followers_count')
  gender=content.get('userInfo').get('gender')
  urank=content.get('userInfo').get('urank')
  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=1
  while True:
    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,proxy_addr)
      content=json.loads(data).get('data')
      cards=content.get('cards')
      if(len(cards)>0):
        for j in range(len(cards)):
          print("-----正在爬取第"+str(i)+"页,第"+str(j)+"条微博------")
          card_type=cards[j].get('card_type')
          if(card_type==9):
            mblog=cards[j].get('mblog')
            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')
            with open(file,'a',encoding='utf-8') as fh:
              fh.write("----第"+str(i)+"页,第"+str(j)+"条微博----"+"\n")
              fh.write("微博地址:"+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
    except Exception as e:
      print(e)
      pass
if __name__=="__main__":
  file=id+".txt"
  get_userInfo(id)
  get_weibo(id,file)

爬取结果

Python爬虫爬取新浪微博内容示例【基于代理IP】

Python爬虫爬取新浪微博内容示例【基于代理IP】

更多关于Python相关内容可查看本站专题:《Python Socket编程技巧总结》、《Python正则表达式用法总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
python完成FizzBuzzWhizz问题(拉勾网面试题)示例
May 05 Python
pygame学习笔记(1):矩形、圆型画图实例
Apr 15 Python
Python判断值是否在list或set中的性能对比分析
Apr 16 Python
Python 序列的方法总结
Oct 18 Python
Python中selenium实现文件上传所有方法整理总结
Apr 01 Python
django初始化数据库的实例
May 27 Python
解决每次打开pycharm直接进入项目的问题
Oct 28 Python
Python虚拟环境的原理及使用详解
Jul 02 Python
pandas factorize实现将字符串特征转化为数字特征
Dec 19 Python
keras导入weights方式
Jun 12 Python
用python批量解压带密码的压缩包
May 31 Python
使用python生成大量数据写入es数据库并查询操作(2)
Sep 23 Python
OpenCV+python手势识别框架和实例讲解
Aug 03 #Python
Windows下将Python文件打包成.EXE可执行文件的方法
Aug 03 #Python
Python测试网络连通性示例【基于ping】
Aug 03 #Python
python版opencv摄像头人脸实时检测方法
Aug 03 #Python
python 读取摄像头数据并保存的实例
Aug 03 #Python
python+opencv+caffe+摄像头做目标检测的实例代码
Aug 03 #Python
python调用摄像头显示图像的实例
Aug 03 #Python
You might like
php for 循环语句使用方法详细说明
2010/05/09 PHP
PHP 缓存实现代码及详细注释
2010/05/16 PHP
关于php循环跳出的问题
2013/07/01 PHP
PHP全局变量与超级全局变量区别分析
2016/04/01 PHP
PHP中include()与require()的区别说明
2017/02/14 PHP
php操作access数据库的方法详解
2017/02/22 PHP
PHP 自动加载类原理与用法实例分析
2020/04/14 PHP
分享一个我自己写的ToolTip提示插件(附源码)
2013/01/20 Javascript
jQuery动画特效实例教程
2014/08/29 Javascript
JS实现很酷的水波文字特效实例
2015/02/26 Javascript
第一次接触神奇的Bootstrap菜单和导航
2016/08/01 Javascript
12 款 JS 代码测试必备工具(翻译)
2016/12/13 Javascript
如何使用Bootstrap 按钮实例详解
2017/03/29 Javascript
vue2的todolist入门小项目的详细解析
2017/05/11 Javascript
mui开发中获取单选按钮、复选框的值(实例讲解)
2017/07/24 Javascript
基于Vue组件化的日期联动选择器功能的实现代码
2018/11/30 Javascript
基python实现多线程网页爬虫
2015/09/06 Python
Python+django实现简单的文件上传
2016/08/17 Python
python 数据的清理行为实例详解
2017/07/12 Python
Python关于反射的实例代码分享
2020/02/20 Python
Python中常用的os操作汇总
2020/11/05 Python
python如何调用php文件中的函数详解
2020/12/29 Python
使用bandit对目标python代码进行安全函数扫描的案例分析
2021/01/27 Python
Hunkemöller瑞士网上商店:欧洲最大的内衣品牌之一
2018/12/03 全球购物
学生打架检讨书大全
2014/01/23 职场文书
物流管理毕业生自荐信范文
2014/03/15 职场文书
新年团拜会主持词
2014/04/02 职场文书
《诚实与信任》教学反思
2014/04/10 职场文书
意向协议书范本
2014/04/23 职场文书
大学生个人先进事迹材料范文
2014/05/03 职场文书
市场督导岗位职责
2015/04/10 职场文书
离婚上诉状范文
2015/05/23 职场文书
导游词之上海东方明珠塔
2019/09/25 职场文书
python爬虫请求库httpx和parsel解析库的使用测评
2021/05/10 Python
mysql中整数数据类型tinyint详解
2021/12/06 MySQL
详解MySQL的内连接和外连接
2023/05/08 MySQL