pyhton微博爬虫(2)——获取微博用户关注列表
2017-08-04 00:54
639 查看
本文的主要目标是获取微博用户关注列表以及关注列表中各微博用户的ID、昵称、详情链接、粉丝数、关注数等关键信息。
实现代码如下所示:
运行结果如下图所示:
mysql数据库中的数据存储如下图所示:
实现代码如下所示:
# -*- coding: utf-8 -*- """ Created on Thu Aug 3 20:59:53 2017 @author: Administrator """ import requests import json import time import random import pymysql.cursors def crawlDetailPage(url,page): #读取微博网页的JSON信息 req = requests.get(url) jsondata = req.text data = json.loads(jsondata) #获取每一条页的数据 content = data['cards'] #print(content) #循环输出每一页的关注者各项信息 for i in content: followingId = i['user']['id'] followingName = i['user']['screen_name'] followingUrl = i['user']['profile_url'] followersCount = i['user']['followers_count'] followCount = i['user']['follow_count'] print("---------------------------------") print("用户ID为:{}".format(followingId)) print("用户昵称为:{}".format(followingName)) print("用户详情链接为:{}".format(followingUrl)) print("用户粉丝数:{}".format(followersCount)) print("用户关注数:{}".format(followCount)) ''' 数据库操作 ''' #获取数据库链接 connection = pymysql.connect(host = 'localhost', user = 'root', password = '123456', db = 'weibo', charset = 'utf8mb4') try: #获取会话指针 with connection.cursor() as cursor: #创建sql语句 sql = "insert into `following` (`followingId`,`followingName`,`followingUrl`,`followersCount`,`followCount`) values (%s,%s,%s,%s,%s)" #执行sql语句 cursor.execute(sql,(followingId,followingName,followingUrl,followersCount,followCount)) #提交数据库 connection.commit() finally: connection.close() for i in range(1,11): print("正在获取第{}页的关注列表:".format(i)) #微博用户关注列表JSON链接 url = "https://m.weibo.cn/api/container/getSecond?containerid=1005052164843961_-_FOLLOWERS&page=" + str(i) crawlDetailPage(url,i) #设置休眠时间 t = random.randint(31,33) print("休眠时间为:{}s".format(t)) time.sleep(t)
运行结果如下图所示:
mysql数据库中的数据存储如下图所示:
相关文章推荐
- Python 获取新浪微博指定用户的微博列表
- pyhton微博爬虫(1)——获取知乎官方账号的微博数据
- Python:获取新浪微博用户的收听列表和粉丝列表
- 获取weibo用户所有的关注列表
- Python:获取新浪微博用户的收听列表和粉丝列表
- pyhton微博爬虫(3)——获取微博评论数据
- 求助:php通过新浪微博接口 api 如果获取某一地区下所有用户的微博列表?或使用包含某一关键词的微博列表
- python+微博API获取我的粉丝列表和关注列表信息(只能得到最新的30%)
- python获取ip代理列表爬虫
- 如何科学地蹭热点:用python爬虫获取热门微博评论并进行情感分析
- 国外第三方登录之facebook(登录,获取用户信息,朋友列表等)含demo
- 基于LDA对关注的微博用户进行聚类
- 获取QQ群用户列表
- Scrapy爬虫 - 获取知乎用户数据
- python爬虫三:获取一个网易用户的所有图片(selenium+phantomjs)
- [Python爬虫] Selenium爬取新浪微博客户端用户信息、热点话题及评论 (上)
- (转)C#开发微信门户及应用(4)--关注用户列表及详细信息管理
- PHP第三方登录之微博登录并获取用户信息
- 运维学python之爬虫高级篇(七)scrapy爬取知乎关注用户存入mongodb
- 微信公众号--获取用户列表