立即登录

注册账号

联系我们

2022年3月29日 09:33

12.1 数据存储_mongodb

案例: 当请求的数据不是JSON格式时,如何保存到mongo

技巧: 自己构建一个列表包含多个字典的数据结构

import requests
from lxml import etree
import pymongo
url = "https://news.hebust.edu.cn/zhxw/index.htm"
r = requests.get(url)
r.encoding = 'utf-8'
html = etree.HTML(r.text)
date = html.xpath('//span[@class="date"]/text()')
year = html.xpath('//span[@class="year"]/text()')
# 将年(2022-3)和日期(28)合并成2022-3-28
dates = [f"{i}-{j}" for i,j in zip(year,date)]
title = html.xpath('//span[@class="newsTxt02"]/a/text()')
# 构建data对象,包含多个字典的列表
data = [{
    "日期":i,
    "标题":j
} for i,j in zip(dates,title)]
client = pymongo.MongoClient('localhost',27017) # 连接客户端
news = client['news'] # 连接数据库,注意:不能创建
keda = news['keda'] # 创建集合,注意:可以连接或创建
keda.insert_many(data) # 插入数据

 

留言

给我留言