python微博爬虫(scrapy)示例源码

上传:red_33136 浏览: 19 推荐: 0 文件:.zip 大小:21.34 KB 上传时间:2021-05-20 23:53:38 版权申诉

class WeiboSpider(Spider): name = 'weibocn' allowed_domains = ['m.weibo.cn'] user_url = 'https://m.weibo.cn/api/container/getIndex?uid={uid}&type=uid&value={uid}&containerid=100505{uid}' follow_url = 'https://m.weibo.cn/api/container/getIndex?containerid=231051_-_followers_-_{uid}&page={page}' fan_url = 'https://m.weibo.cn/api/container/getIndex?containerid=231051_-_fans_-_{uid}&page={page}' weibo_url = 'https://m.weibo.cn/api/container/getIndex?uid={uid}&type=uid&page={page}&containerid=107603{uid}' start_users = ['3217179555', '1742566624', '2282991915', '1288739185', '3952070245', '5878659096'] def start_requests(self): for uid in self.start_users: yield Request(self.user_url.format(uid=uid), callback=self.parse_user) def parse_user(self, response): """ 解析用户信息 :param response: Response对象 """ self.logger.debug(response) result = json.loads(response.text) if result.get('data').get('userInfo'): user_info = result.get('data').get('userInfo') user_item = UserItem() field_map = { 'id': 'id', 'name': 'screen_name', 'avatar': 'profile_image_url', 'cover': 'cover_image_phone', 'gender': 'gender', 'description': 'description', 'fans_count': 'followers_count', 'follows_count': 'follow_count', 'weibos_count': 'statuses_count', 'verified': 'verified', 'verified_reason': 'verified_reason', 'verified_type': 'verified_type' } for field, attr in field_map.items(): user_item[field] = user_info.get(attr) yield user_item # 关注 uid = user_info.get('id') yield Request(self.follow_url.format(uid=uid, page=1), callback=self.parse_follows, meta={'page': 1, 'uid': uid}) # 粉丝 yield Request(self.fan_url.format(uid=uid, page=1), callback=self.parse_fans, meta={'page': 1, 'uid': uid}) # 微博 yield Request(self.weibo_url.format(uid=uid, page=1), callback=self.parse_weibos, meta={'page': 1, 'uid': uid}) def parse_follows(self, response): """ 解析用户关注 :param response: Response对象 """ result = json.loads(response.text) if result.get('ok') and result.get('data').get('cards') and len(result.get('data').get('cards')) and result.get('data').get('cards')[-1].get( 'card_group'): # 解析用户 follows = result.get('data').get('cards')[-1].get('card_group') for follow in follows: if follow.get('user'): uid = follow.get('user').get('id') yield Request(self.user_url.format(uid=uid), callback=self.parse_user) uid = response.meta.get('uid') # 关注列表 user_relation_item = UserRelationItem() follows = [{'id': follow.get('user').get('id'), 'name': follow.get('user').get('screen_name')} for follow in follows] user_relation_item['id'] = uid user_relation_item['follows'] = follows user_relation_item['fans'] = [] yield user_relation_item # 下一页关注 page = response.meta.get('page') 1 yield Request(self.follow_url.format(uid=uid, page=page), callback=self.parse_follows, meta={'page': page, 'uid': uid}) def parse_fans(self, response): """ 解析用户粉丝 :param response: Response对象 """ result = json.loads(response.text) if result.get('ok') and result.get('data').get('cards') and len(result.get('data').get('cards')) and result.get('data').get('cards')[-1].get( 'card_group'): # 解析用户 fans = result.get('data').get('cards')[-1].get('card_group') for fan in fans: if fan.get('user'): uid = fan.get('user').get('id') yield Request(self.user_url.format(uid=uid), callback=self.parse_user) uid = response.meta.get('uid') # 粉丝列表 user_relation_item = UserRelationItem() fans = [{'id': fan.get('user').get('id'), 'name': fan.get('user').get('screen_name')} for fan in fans] user_relation_item['id'] = uid user_relation_item['fans'] = fans user_relation_item['follows'] = [] yield user_relation_item # 下一页粉丝 page = response.meta.get('page') 1 yield Request(self.fan_url.format(uid=uid, page=page), callback=self.parse_fans, meta={'page': page, 'uid': uid}) def parse_weibos(self, response): """ 解析微博列表 :param response: Response对象 """ result = json.loads(response.text) if result.get('ok') and result.get('data').get('cards'): weibos = result.get('data').get('cards') for weibo in weibos: mblog = weibo.get('mblog') if mblog: weibo_item = WeiboItem() field_map = { 'id': 'id', 'attitudes_count': 'attitudes_count', 'comments_count': 'comments_count', 'reposts_count': 'reposts_count', 'picture': 'original_pic', 'pictures': 'pics', 'created_at': 'created_at', 'source': 'source', 'text': 'text', 'raw_text': 'raw_text', 'thumbnail': 'thumbnail_pic', } for field, attr in field_map.items(): weibo_item[field] = mblog.get(attr) weibo_item['user'] = response.meta.get('uid') yield weibo_item # 下一页微博 uid = response.meta.get('uid') page = response.meta.get('page') 1 yield Request(self.weibo_url.format(uid=uid, page=page), callback=self.parse_weibos, meta={'uid': uid, 'page': page})

上传资源
用户评论
相关推荐
python爬虫示例源码lxml
python微博爬虫示例源码lxml
.py
7.81 KB
2021-05-20 22:20
Python scrapy爬虫入门三scrapy爬虫示例
1 爬虫示例 要实现爬虫功能,只要执行四个步骤: 定义spider 类 确定 spider 的名称(name) 获取初始化请求(start_request) 解析数据 parse() 1.1 示例1
PDF
70KB
2021-02-24 23:00
Python基于python36的爬虫scrapy
基于python3.6的微博爬虫(scrapy)
.ZIP
27KB
2020-08-17 14:54
Python实现爬虫源码.rar
功能:连续爬取一个或多个新浪微博用户(如胡歌、迪丽热巴、郭碧婷)的数据,并将结果信息写入文件或数据库。写入信息几乎包括了用户微博的所有数据,主要有用户信息和微博信息两大类,前者包含用户昵称、关注数、粉
RAR
0B
2020-05-18 03:09
Python爬虫scrapy框架Cookie池Cookie池的使用
Python爬虫scrapy框架Cookie池(微博Cookie池)的使用
pdf
831.79 KB
2021-07-06 03:05
python新浪爬虫
python新浪微博爬虫python新浪微博爬虫python新浪微博爬虫python新浪微博爬虫python新浪微博爬虫python新浪微博爬虫python新浪微博爬虫python新浪微博爬虫
RAR
0B
2019-05-31 04:50
Python实现爬虫
使用Python实现微博爬虫 1. 需要爬取的页面URL存取在WeiboSpider/DataBase/UrlRecord.db, 该文件为sqlite数据库文件, URL数据会在爬取的过程中持续添加
RAR
0B
2019-01-20 23:19
Python Scrapy爬虫爬取信公众号热门消息
爬取微博需要以字典的形式设置自己的cookie。用于初学者学习,分别用了bf4和xpath,数据处理还不完善,存入了mysql数据库,改写sql语句就可以写入自己的表中
RAR
0B
2019-07-17 23:47
pythonscrapy urllib等爬虫示例
该压缩包内包含python环境下的scrapy等爬虫实例(豆瓣/腾讯等网址爬取)。Scrapy,Python开发的一个快速、高层次的屏幕抓取和web抓取框架,用于抓取web站点并从页面中提取结构化的数
zip
0B
2019-09-06 20:45
WeiboSpider这是由scrapy建造的新浪蜘蛛爬虫持续维护源码
中文说明| 微博蜘蛛 持续维护的新浪微博爬虫 :rocket: :rocket: :rocket: 项目说明 版本说明 该项目分为2个分支,连续不同的需要 分支 特色 抓取量 单账号,单IP,单机器
ZIP
5.23MB
2021-04-19 09:32
python爬虫通过ID
通过个人微博/官方微博ID,获取一定时间段以内该用户所转发的所有内容并按一定格式存储到本地。
PY
0B
2019-03-28 13:48
Python新浪爬虫ScrapyRedis
此项目和QQ空间爬虫类似,主要爬取新浪微博用户的个人信息、微博信息、粉丝和关注(详细见此)。
ZIP
0B
2020-05-28 19:05
Scrapy爬虫根据关键词爬取相关帖子信息
主要使用Python中第三方库Scrapy爬虫框架首先你需要阅读README.md文件说明然后输入你的微博cookie然后输入关键词爬取日期等等信息最后运行即可.
rar
6.42MB
2023-01-12 12:53
python scrapy爬虫详解
该书是英文版的,适合英语较好的同学学习,书中对原理性的内容讲解很详细有助于深入理解scrapy爬虫,具体代码和举例也有不少
PDF
0B
2019-09-28 20:00
python爬虫scrapy Mongo
python爬虫基础学习,从基础学习,涉及到scrapy,Mongo,scrapy_redis等相关的基础知识
TXT
75B
2020-09-02 15:43