用python写网络爬虫-爬取新浪微博评论

本文介绍了一种使用Python爬取新浪微博评论的方法,通过访问m.weibo.cn简化操作并直接获取微博ID,利用json模块解析动态加载的数据,编写了字符优化函数去除干扰字符。
部署运行你感兴趣的模型镜像

新浪微博需要登录才能爬取,这里使用m.weibo.cn这个移动端网站即可实现简化操作,用这个访问可以直接得到的微博id。

分析新浪微博的评论获取方式得知,其采用动态加载。所以使用json模块解析json代码

单独编写了字符优化函数,解决微博评论中的嘈杂干扰字符

本函数是用python写网络爬虫的终极目的,所以采用函数化方式编写,方便后期优化和添加各种功能

# -*- coding:gbk -*-
import re
import requests
import json
from lxml import html
#测试微博4054483400791767
comments=[]

def get_page(weibo_id):
    url='https://m.weibo.cn/status/{}'.format(weibo_id)
    html=requests.get(url).text
    regcount=r'"comments_count": (.*?),'
    comments_count=re.findall(regcount,html)[-1]
    comments_count_number=int(comments_count)
    page=int(comments_count_number/10)
    return page-1

def opt_comment(comment):
    tree=html.fromstring(comment)
    strcom=tree.xpath('string(.)')
    reg1=r'回复@.*?:'
    reg2=r'回覆@.*?:'
    reg3=r'//@.*'
    newstr=''
    comment1=re.subn(reg1,newstr,strcom)[0]
    comment2=re.subn(reg2,newstr,comment1)[0]
    comment3=re.subn(reg3,newstr,comment2)[0]
    return comment3

def get_responses(id,page):
    url="https://m.weibo.cn/api/comments/show?id={}&page={}".format(id,page)
    response=requests.get(url)
    return response

def get_weibo_comments(response):
    json_response=json.loads(response.text)
    for i in range(0,len(json_response['data'])):
        comment=opt_comment(json_response['data'][i]['text'])
        comments.append(comment)


weibo_id=input("输入微博id,自动返回前5页评论:")
weibo_id=int(weibo_id)
print('\n')
page=get_page(weibo_id)
for page in range(1,page+1):
    response=get_responses(weibo_id,page)
    get_weibo_comments(response)

for com in comments:
    print(com)
print(len(comments))

您可能感兴趣的与本文相关的镜像

Python3.11

Python3.11

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

好的,我知道你想了解如何用 Python 爬取新浪微博评论。以下是一些步骤供您参考: 1. 安装必要的库 在使用 Python 爬取微博评论前,您需要安装一些必要的库。其中,requests 和 BeautifulSoup 库是必不可少的。 您可以使用以下命令在终端中安装它们: ``` pip install requests pip install beautifulsoup4 ``` 2. 登录微博并获取 cookie 在爬取微博评论之前,您需要登录您的微博账号并获取 cookie。 您可以使用 Chrome 浏览器登录您的微博账号。然后,按 F12 打开开发者工具,切换到“Network”选项卡并刷新页面。在“Filter”栏中输入“comment”,然后点击“XHR”选项卡。 接下来,您可以在“Headers”选项卡中找到“Request Headers”部分,复制“Cookie”字段的值。 3. 构造请求 URL 在获取 cookie 后,您需要构造请求 URL。以下是一个示例 URL:https://weibo.cn/comment/Jf3bhk1V5?uid=123456&page=1 其中,Jf3bhk1V5 是微博的 ID,123456 是用户的 ID,page 是评论的页数。 您可以使用 requests 库发送 GET 请求并传递 cookie、微博 ID 和页数参数。 以下是一个示例代码: ``` import requests from bs4 import BeautifulSoup cookie = 'your_cookie_here' url = 'https://weibo.cn/comment/Jf3bhk1V5?uid=123456&page=1' headers = { 'Cookie': cookie } response = requests.get(url, headers=headers) soup = BeautifulSoup(response.text, 'html.parser') # 解析评论 ``` 4. 解析评论 最后,您需要解析页面上的评论。您可以使用 BeautifulSoup 库来解析 HTML。 以下是一个示例代码: ``` comments = soup.find_all('div', class_='c') for comment in comments: # 获取评论内容 content = comment.find('span', class_='ctt').text # 获取评论时间 time = comment.find('span', class_='ct').text # 获取评论用户 user = comment.find('a', class_='nk').text # 输出评论信息 print(f'{user}({time}): {content}\n') ``` 以上就是用 Python 爬取新浪微博评论的一些步骤。请注意,爬取微博评论可能会违反微博使用协议,因此请谨慎使用
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值