当前位置: 首页 > news >正文

豆瓣书摘 | 爬虫 | Python

获取豆瓣书摘,存入MongoDB中。

import logging
import timeimport requests
from bs4 import BeautifulSoup
from pymongo import MongoClientheaders = {'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7','accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6','cache-control': 'max-age=0','priority': 'u=0, i','sec-ch-ua': '"Chromium";v="130", "Microsoft Edge";v="130", "Not?A_Brand";v="99"','sec-ch-ua-mobile': '?0','sec-ch-ua-platform': '"Windows"','sec-fetch-dest': 'document','sec-fetch-mode': 'navigate','sec-fetch-site': 'none','sec-fetch-user': '?1','upgrade-insecure-requests': '1','user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0',
}params = {'sort': 'score','start': 0,
}
# 连接到 MongoDB 服务器(假设在本地运行,默认端口 27017)
client = MongoClient('localhost', 27017)# 选择数据库(如果数据库不存在,MongoDB 会在插入数据时自动创建)
db = client['douban_database']# 选择集合(如果集合不存在,MongoDB 会在插入数据时自动创建)
collection = db['blockquotes_1009393']
for start in range(0, 1260, 20):params['start'] = startresponse = requests.get('https://book.douban.com/subject/1009393/blockquotes', params=params, headers=headers)text = response.textsoup = BeautifulSoup(text, 'lxml')if len(soup.findAll("div", attrs={"class": "blockquote-list"})) == 0:logging.error("blockquote-list is not exist")exit(1)blockquote_list = soup.findAll("div", attrs={"class": "blockquote-list"})[0]if blockquote_list is None:logging.error("blockquote-list None")exit(1)figures = blockquote_list.findAll("figure")for figure in figures:if figure is None:logging.warning("figure is None")continuedata = {'author_avatar': None,'author_name': None,'likes': None,'datetime': None,'page_reference': None}try:data['author_avatar'] = figure.find('img')['src']except:data['author_avatar'] = Nonelogging.error(figure)try:data['author_name'] = figure.find('a', class_='author-name').text.strip()except:data['author_name'] = Nonelogging.error(figure)try:data['likes'] = figure.find('span').text.strip().replace('赞', '')except:data['likes'] = Nonelogging.error(figure)try:data['datetime'] = figure.find('datetime').text.strip()except:data['datetime'] = Nonelogging.error(figure)try:data['page_reference'] = figure.find('figcaption')['title']except:data['page_reference'] = Nonelogging.error(figure)try:blockquote_extra = figure.find('div', class_='blockquote-extra')a_href = figure.find('a')blockquote_extra.decompose()a_href.decompose()content = figure.text.strip().replace('()', '')# print(content)data['content'] = contentexcept:data['content'] = Nonelogging.error(figure)try:passcollection.insert_one(data)except Exception as e:print(e)time.sleep(3)

效果图:
存入数据库效果图

http://www.lryc.cn/news/488286.html

相关文章:

  • Oracle数据库物理存储结构管理
  • java——Map接口
  • 量子计算机全面解析:技术、应用与未来
  • IDEA相关(包括但不限于快捷键,使用技巧)成长笔记
  • 【再谈设计模式】适配器模式 ~接口兼容的桥梁
  • 使用Cursor和Claude AI打造你的第一个App
  • 粗读Apache Paimon 的基本概念及其组成结构
  • c++调用 c# dll 通过 P/Invoke (详细避坑)
  • 李春葆《数据结构》——图相关代码
  • Linux驱动开发第2步_“物理内存”和“虚拟内存”的映射
  • 告别多品牌乱战,吉利开始觉醒
  • Target-absent Human Attention
  • <QNAP 453D QTS-5.x> 日志记录:在 Docker 中运行的 Flask 应用安装 自签名 SSL 证书 解决 Chrome 等浏览器证书安全
  • 通过huggingface-cli下载Hugging Face上的公开数据集或模型至本地
  • 论文阅读——Intrusion detection systems using longshort‑term memory (LSTM)
  • SparkSQL的执行过程:从源码角度解析逻辑计划、优化计划和物理计划
  • Leetcode打卡:新增道路查询后的最短距离II
  • Spring Web入门练习
  • 计算机毕业设计 | SpringBoot+vue汽车资讯网站 汽车购买咨询管理系统(附源码+论文)
  • stm32下的ADC转换(江科协 HAL版)
  • 解决IntelliJ IDEA的Plugins无法访问Marketplace去下载插件
  • react 如何修改弹出的modal的标题
  • C#中的二维数组的应用:探索物理含义与数据结构的奇妙融合
  • HTML5拖拽API学习 托拽排序和可托拽课程表
  • 内容补充页(相关公式解释)
  • vue中动态渲染静态图片资源
  • 管伊佳ERP,原名华夏ERP,一个简约易上手的国产ERP系统
  • 学习虚幻C++开发日志——委托(持续更新中)
  • 开窗函数 - first_value/last_value
  • 「一」HarmonyOS端云一体化概要