python爬虫——使用selenium爬取微博数据(一)

python爬虫——使用selenium爬取微博数据(二)

写在前面

之前因为在组里做和nlp相关的项目,需要自己构建数据集,采用selenium爬取了几十万条微博数据,学习了很多,想在这里分享一下如何用selenium爬取微博上任何你想要的数据,这里默认大家都会最基本的python操作哈~

为什么要用selenium

selenium库的好处:Selenium 测试直接在浏览器中运行,就像真实用户所做的一样。用通俗的话来说,当你在爬取微博数据的时候,就仿佛有一只 “无形的手”帮你登录微博,搜索你想要的内容,下载你想要的图片等等,这只手就是你写的代码啦~

安装selenium和chromedriver

废话不多说,我们需要先安装两个工具

selenium

直接pip或conda安装就可以啦

conda install selenium

chromedriver

chromedriver是操作chrome的驱动,首先我们要找到和我们的浏览器适配的chromedriver版本

selenium除了chrome还有很多浏览器都可以配合使用的,比如firefox啥的都可以滴,大家不想使用chrome的话可以去找找相关博客安装,大同小异~

  1. 查看浏览器版本,在浏览器中输入chrome://version/就能看到啦,我的版本是86.0.4240.111
    在这里插入图片描述
  2. 查找chrome浏览器对应的版本chromedriver驱动版本,可以在这里查看

在这里插入图片描述
点进去,选择适合自己系统的压缩包下载就行
在这里插入图片描述

  1. 之后把压缩包中的chromedriver.exe解压缩到chrome文件夹中

其实可以解压到任何一个路径,只要最后添加好环境变量就好了,或者直接使用绝对路径~

在这里插入图片描述
4. 添加环境变量(此电脑-属性-高级系统设置-环境变量)
在这里插入图片描述

测试一下

这个时候配置工作就完成啦,可以运行一下测试代码~

from selenium import webdriver
driver = webdriver.Chrome()
#上面是配置好环境变量的写法,像下面这样用绝对路径也是ok滴
#driver = webdriver.Chrome('C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe')
driver.get('https://www.baidu.com')

别忘了改成自己的路径

下一篇会正式带大家使用selenium在微博上爬取你喜欢的明星的所有微博和图片~有问题欢迎大家及时指出!

  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
以下是使用Python爬取微博关键词数据的步骤: 1. 安装必要的库 使用Python爬取微博数据需要安装以下库: - requests:用于发送HTTP请求 - BeautifulSoup4:用于解析HTML文档 - selenium:用于模拟浏览器行为 - pandas:用于数据处理和分析 安装方法: ```python pip install requests beautifulsoup4 selenium pandas ``` 2. 登录微博 爬取微博数据需要登录微博账号。这里使用selenium模拟浏览器登录微博。 ```python from selenium import webdriver # 启动浏览器 browser = webdriver.Chrome() # 打开微博登录页面 browser.get('https://passport.weibo.cn/signin/login') # 输入账号密码 username_input = browser.find_element_by_id('loginName') username_input.send_keys('your_username') password_input = browser.find_element_by_id('loginPassword') password_input.send_keys('your_password') # 点击登录按钮 login_button = browser.find_element_by_id('loginAction') login_button.click() ``` 3. 搜索关键词 登录成功后,需要搜索关键词。这里使用requests发送HTTP请求,获取搜索结果页面HTML代码,并使用BeautifulSoup解析HTML。 ```python import requests from bs4 import BeautifulSoup # 搜索关键词 keyword = 'Python' search_url = 'https://s.weibo.com/weibo?q=' + keyword # 发送HTTP请求,获取搜索结果页面HTML代码 response = requests.get(search_url) html = response.text # 解析HTML soup = BeautifulSoup(html, 'html.parser') ``` 4. 解析搜索结果 解析搜索结果,获取微博列表。每个微博包含以下信息: - 用户名 - 用户ID - 微博内容 - 发布时间 - 转发数 - 评论数 - 点赞数 ```python # 获取微博列表 feed_list = soup.select('.card-wrap .card .card-feed') # 解析每个微博 for feed in feed_list: # 用户名和用户ID user = feed.select_one('.info .name') username = user.text.strip() userid = user['href'].split('/')[-1] # 微博内容 content = feed.select_one('.content .txt') content_text = content.text.strip() # 发布时间 time = feed.select_one('.content .from') time_text = time.text.strip() # 转发数、评论数、点赞数 actions = feed.select_one('.card-act') reposts = actions.select_one('.btn-like span') reposts_text = reposts.text.strip() comments = actions.select_one('.btn-comment span') comments_text = comments.text.strip() likes = actions.select_one('.btn-like span') likes_text = likes.text.strip() ``` 5. 存储数据 将获取到的微博数据存储到CSV文件中,便于后续分析。 ```python import pandas as pd # 存储数据 data = { '用户名': usernames, '用户ID': userids, '微博内容': contents, '发布时间': times, '转发数': reposts, '评论数': comments, '点赞数': likes } df = pd.DataFrame(data) df.to_csv('weibo.csv', index=False, encoding='utf-8') ```
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值