存到mangoDB

import requests
import datetime
from bs4 import BeautifulSoup
from pymongo import MongoClient
client = MongoClient('localhost',27017)
db = client.blog_database
collection = db.blog

link = "....."          
headers = {'User-Agent' : 'Mozilla/5.0 (Windows; U; Windows NT  6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}
r = requests.get(link, headers= headers)

soup = BeautifulSoup(r.text, "lxml")
title_list = soup.find_all("h1", class_="post-title")
for eachone in title_list:
    url = eachone.a['href']
    title = eachone.a.text.strip()
    post = {"url": url,
        "title": title,
        "date": datetime.datetime.utcnow()}
    collection.insert_one(post)

在上面的代码中,首先将爬虫获取的数据存入post的字典中,然后使用insert_one加入集合collection中。进入目录C:\Program Files\MongoDB\Server\3.4\bin,双击MongoDB.exe打开,输入:

use blog_database
db.blog.find().pretty()

这样就能够查询数据集合的数据了。

你可能感兴趣的:(存到mangoDB)