如何用python实现爬取CSDN热门评论URL并存入redis
作者:吹吹风会发光
时间:2023-04-30
浏览:5
Redis Assistant
Redis Assistant 是一款为Redis设计的高效可视化管理与监控工具,提供多种操作模式,高效处理服务器数据。日常使用里的持续防护和信息管理会更突出,适合把安全控制放进长期使用流程中的场景。
¥19
Windows/macOS/Linux
2026-08-22
一、配置webdriver下载谷歌浏览器驱动,并配置好importtimeimportrandomfromPILimportImagefromseleniumimportwebdriverfromselenium.webdriver.common.byimportByfromselenium.webdriver.support.uiimportWebDriverWaitfromselenium.webdriver.supportimportexpected_conditionsasECif_
本文目录
一、配置webdriver
下载谷歌浏览器驱动,并配置好
import timeimport randomfrom PIL import Imagefrom selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECif __name__ == '__main__':options = webdriver.ChromeOptions()options.binary_location = r'C:UsershhhAppDataLocalGoogleChromeApplication谷歌浏览器.exe'# driver=webdriver.Chrome(executable_path=r'D:360Chromechromedriverchromedriver.exe')driver = webdriver.Chrome(options=options)#以java模块为例driver.get('https://www.csdn.net/nav/java')for i in range(1,20):driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")time.sleep(2)二、获取URL
from bs4 import BeautifulSoupfrom lxml import etree
html = etree.HTML(driver.page_source)# soup = BeautifulSoup(html, 'lxml')# soup_herf=soup.find_all("#feedlist_id > li:nth-child(1) > div > div > h2 > a")# soup_herftitle = html.xpath('//*[@id="feedlist_id"]/li/div/div/h2/a/@href')可以看到,一下爬取了很多,速度非常快
三、写入Redis
导入redis包后,配置redis端口和redis数据库,用rpush函数写入
打开redis
import redis
r_link = redis.Redis(port='6379', host='localhost', decode_responses=True, db=1)for u in title:print("准备写入{}".format(u))r_link.rpush("csdn_url", u)print("{}写入成功!".format(u))print('=' * 30, 'n', "共计写入url:{}个".format(len(title)), 'n', '=' * 30)
大功告成!
在Redis Desktop Manager中可以看到,爬取和写入都是非常的快。
要使用只需用rpop出栈就OK
one_url = r_link.rpop("csdn_url)")while one_url:print("{}被弹出!".format(one_url))
作者最新文章
ABBYY FineReader内容导入 文件添加步骤
2026-11-04 20:14
Agent Space智能体如何进行功能测试
2026-11-01 08:45
Proxifier怎么卸载:Windows标准版彻底移除与配置清理指南
2026-10-30 09:37
七彩虹 iGame RTX 5070 Ti Ultra SFF 显卡图赏:嘻哈涂鸦风,设计更潮酷
2026-10-26 16:48
viso如何导入word的详细操作步骤教程
2026-10-20 16:03
下一篇:
使用Python自动下载和更换桌面壁纸
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多

































