python爬取微博恶评_python爬取微博评论的实例讲解
python爬虫是程序员们一定会掌握的知识,练习python爬虫时,很多人会选择爬取微博练手。python爬虫微博根据微博存在于不同媒介上,所爬取的难度有差异,无论是python新入手的小白,还是已经熟练掌握的程序员,可以拿来练手。本文介绍python爬取微博评论的代码实例。
一、爬虫微博
与QQ空间爬虫类似,可以爬取新浪微博用户的个人信息、微博信息、粉丝、关注和评论等。
爬虫抓取微博的速度可以达到 1300万/天 以上,具体要视网络情况。
难度程度排序:网页端>手机端>移动端。微博端就是最好爬的微博端。
二、python爬虫爬取微博评论
第一步:确定评论用户的id
# -*- coding:utf-8 -*-
import requests
import re
import time
import pandas as pd
urls = 'https://m.weibo.cn/api/comments/show?id=4073157046629802&page={}'
headers = {'Cookies':'Your cookies',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36'}
第二步:找到html标签
tags = re.compile('?\w+[^>]*>')
第三步:设置提取评论function
def get_comment(url):
j = requests.get(url, headers=headers).json()
comment_data = j['data']['data']
for data in comment_data:
try:
第四步:利用正则表达式去除文本中的html标签
comment = tags.sub('', data['text']) # 去掉html标签
reply = tags.sub('', data['reply_text'])
weibo_id = data['id']
reply_id = data['reply_id']
comments.append(comment)
comments.append(reply)
ids.append(weibo_id)
ids.append(reply_id)
第五步:爬取评论
df = pd.DataFrame({'ID': ids, '评论': comments})
df = df.drop_duplicates()
df.to_csv('观察者网.csv', index=False, encoding='gb18030')
实例扩展:
# -*- coding: utf-8 -*-
# Created : 2018/8/26 18:33
# author :GuoLi
import requests
import json
import time
from lxml import etree
import html
import re
from bs4 import BeautifulSoup
class Weibospider:
def __init__(self):
# 获取首页的相关信息:
self.start_url = 'https://weibo.com/u/5644764907?page=1&is_all=1'
self.headers = {
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"accept-encoding": "gzip, deflate, br",
"accept-language": "zh-CN,zh;q=0.9,en;q=0.8",
"cache-control": "max-age=0",
"cookie": 使用自己本机的cookie,
"referer": "https://www.weibo.com/u/5644764907?topnav=1&wvr=6&topsug=1",
"upgrade-insecure-requests": "1",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36",
}
self.proxy = {
'HTTP': 'HTTP://180.125.70.78:9999',
'HTTP': 'HTTP://117.90.4.230:9999',
'HTTP': 'HTTP://111.77.196.229:9999',
'HTTP': 'HTTP://111.177.183.57:9999',
'HTTP': 'HTTP://123.55.98.146:9999',
}
def parse_home_url(self, url): # 处理解析首页面的详细信息(不包括两个通过ajax获取到的页面)
res = requests.get(url, headers=self.headers)
response = res.content.decode().replace("\\", "")
# every_url = re.compile('target="_blank" href="(/\d+/\w+\?from=\w+&wvr=6&mod=weibotime)" rel="external nofollow" ', re.S).findall(response)
every_id = re.compile('name=(\d+)', re.S).findall(response) # 获取次级页面需要的id
home_url = []
for id in every_id:
base_url = 'https://weibo.com/aj/v6/comment/big?ajwvr=6&id={}&from=singleWeiBo'
url = base_url.format(id)
home_url.append(url)
return home_url
def parse_comment_info(self, url): # 爬取直接发表评论的人的相关信息(name,info,time,info_url)
res = requests.get(url, headers=self.headers)
response = res.json()
count = response['data']['count']
html = etree.HTML(response['data']['html'])
name = html.xpath("//div[@class='list_li S_line1 clearfix']/div[@class='WB_face W_fl']/a/img/@alt") # 评论人的姓名
info = html.xpath("//div[@node-type='replywrap']/div[@class='WB_text']/text()") # 评论信息
info = "".join(info).replace(" ", "").split("\n")
info.pop(0)
comment_time = html.xpath("//div[@class='WB_from S_txt2']/text()") # 评论时间
name_url = html.xpath("//div[@class='WB_face W_fl']/a/@href") # 评论人的url
name_url = ["https:" + i for i in name_url]
comment_info_list = []
for i in range(len(name)):
item = {}
item["name"] = name[i] # 存储评论人的网名
item["comment_info"] = info[i] # 存储评论的信息
item["comment_time"] = comment_time[i] # 存储评论时间
item["comment_url"] = name_url[i] # 存储评论人的相关主页
comment_info_list.append(item)
return count, comment_info_list
def write_file(self, path_name, content_list):
for content in content_list:
with open(path_name, "a", encoding="UTF-8") as f:
f.write(json.dumps(content, ensure_ascii=False))
f.write("\n")
def run(self):
start_url = 'https://weibo.com/u/5644764907?page={}&is_all=1'
start_ajax_url1 = 'https://weibo.com/p/aj/v6/mblog/mbloglist?ajwvr=6&domain=100406&is_all=1&page={0}&pagebar=0&pl_name=Pl_Official_MyProfileFeed__20&id=1004065644764907&script_uri=/u/5644764907&pre_page={0}'
start_ajax_url2 = 'https://weibo.com/p/aj/v6/mblog/mbloglist?ajwvr=6&domain=100406&is_all=1&page={0}&pagebar=1&pl_name=Pl_Official_MyProfileFeed__20&id=1004065644764907&script_uri=/u/5644764907&pre_page={0}'
for i in range(12): # 微博共有12页
home_url = self.parse_home_url(start_url.format(i + 1)) # 获取每一页的微博
ajax_url1 = self.parse_home_url(start_ajax_url1.format(i + 1)) # ajax加载页面的微博
ajax_url2 = self.parse_home_url(start_ajax_url2.format(i + 1)) # ajax第二页加载页面的微博
all_url = home_url + ajax_url1 + ajax_url2
for j in range(len(all_url)):
print(all_url[j])
path_name = "第{}条微博相关评论.txt".format(i * 45 + j + 1)
all_count, comment_info_list = self.parse_comment_info(all_url[j])
self.write_file(path_name, comment_info_list)
for num in range(1, 10000):
if num * 15 < int(all_count) + 15:
comment_url = all_url[j] + "&page={}".format(num + 1)
print(comment_url)
try:
count, comment_info_list = self.parse_comment_info(comment_url)
self.write_file(path_name, comment_info_list)
except Exception as e:
print("Error:", e)
time.sleep(60)
count, comment_info_list = self.parse_comment_info(comment_url)
self.write_file(path_name, comment_info_list)
del count
time.sleep(0.2)
print("第{}微博信息获取完成!".format(i * 45 + j + 1))
if __name__ == '__main__':
weibo = Weibospider()
weibo.run()
到此这篇关于python爬取微博评论的实例讲解的文章就介绍到这了,更多相关python爬虫爬取微博评论内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
本文标题: python爬取微博评论的实例讲解
本文地址: http://www.cppcns.com/jiaoben/python/373972.html
python爬取微博恶评_python爬取微博评论的实例讲解相关推荐
- python爬取微博恶评_Python爬取新浪微博评论数据,了解一下?
开发工具 **Python版本:**3.6.4 相关模块: argparse模块: requests模块: jieba模块: wordcloud模块: 以及一些Python自带的模块. 环境搭建 安装 ...
- python股票回测源码_Python爬虫回测股票的实例讲解
股票和基金一直是热门的话题,很多周围的人都选择不同种类的理财方式.就股票而言,肯定是短时间内收益最大化,这里我们需要用python爬虫的方法,来帮助我们获取一些股票的数据,这样才能更好的买到相应的股票 ...
- python微博爬虫分析_python爬取和分析新浪微博(一):scrapy构建新浪微博榜单、博主及微博信息爬虫...
1. 爬虫项目介绍 爬虫首先基于python scrapy 框架,使用了随机用户代理和IP代理等反爬技巧,将爬取到的微博领域数据,各领域榜单博主信息和博主的微博信息分别存入的mysql数据库对应的表格 ...
- python 数据分析 书籍推荐 知乎_Python 爬取知乎 9674 个问答,揭秘最受欢迎的 98 本书!...
原标题:Python 爬取知乎 9674 个问答,揭秘最受欢迎的 98 本书! 作者 | Yura 责编 | 胡巍巍 高尔基这话有没有道理我不知道, 咱也不敢问, 主要是现在也问不了. 那对我来说,读 ...
- python听歌识曲爬虫_Python爬取网易云音乐热门评论的搜索结果-阿里云开发者社区...
网易云音乐评论爬虫(三):爬取歌曲的全部评论 用过网易云音乐听歌的朋友都知道,网易云音乐每首歌曲后面都有很多评论,热门歌曲的评论更是接近百万或者是超过百万条.现在我就来分享一下如何爬取网易云音乐歌曲的 ...
- python知乎爬虫收藏夹_Python爬取知乎问题收藏夹 爬虫入门
简介 知乎的网站是比较好爬的,没有复杂的反爬手段,适合初学爬虫的人作为练习 因为刚刚入门python,所以只是先把知乎上热门问题的一些主要信息保存到数据库中,待以后使用这些信息进行数据分析,爬取的网页 ...
- python爬取音乐并保存_Python爬取网易云音乐上评论火爆的歌曲
前言 网易云音乐这款音乐APP本人比较喜欢,用户量也比较大,而网易云音乐之所以用户众多和它的歌曲评论功能密不可分,很多歌曲的评论非常有意思,其中也不乏很多感人的评论.但是,网易云音乐并没有提供热评排行 ...
- python百度贴吧 发帖时间_python爬取贴吧帖子
一.介绍 我们常遇到一些很长的贴吧连载帖子想存到本地再看 此文就是运用python爬取指定百度贴吧的帖子并存到本地满足需求 环境:python2.7 目标网页:[长篇连载]剑网3的正史和野史--从头开 ...
- python爬取b站用户_python爬取b站排行榜
爬取b站排行榜并存到mysql中 目的 b站是我平时看得最多的一个网站,最近接到了一个爬虫的课设.首先要选择一个网站,并对其进行爬取,最后将该网站的数据存储并使其可视化. 网站的结构 目标网站:bil ...
最新文章
- Load Balance Tomcat with Nginx and Store Sessions in Redis--reference
- [论文浅析]ICLR2017 跨语言,跨任务,跨领域序列标注
- C++new和delete运算符
- Android 判断应用 第一次启动
- css3实现科技感的呼吸灯效果
- 全球视频监控设备市场规模分析
- 理解js中的原型链,prototype与__proto__的关系
- 信号与系统 chapter9 关于信号与系统中微分方程的求解
- 人工智障学习笔记——机器学习(1)特征工程
- 查看硬件配置的Linux命令,LINUX 查看硬件配置命令的教程
- Struts2实现简单的在线人数统计
- android 腾讯 gt,源码解读腾讯 GT 的性能测试方案
- spark TF-IDF入门
- PHP100视频教程(2012-2013版)下载地址及密码
- AdapterView学习总结
- java碰撞检测代码_java碰撞检测代码
- UE4----GC(垃圾回收)
- 【知识兔】Excel教程之隔行/隔列求和
- Neo4j入门实战之红楼梦知识图谱
- Bilateral Self-unbiased Learning from Biased Implicit Feedback-2022年9月之前最新的推荐系统方向论文