python爬取豆瓣读书网存在mongodb中
时间: 2023-11-04 14:53:51 浏览: 81
Python 爬取豆瓣读书
好的,首先需要安装pymongo,可以使用以下命令安装:
```
pip install pymongo
```
接下来,可以按照以下步骤进行爬取并存储到MongoDB中:
1. 导入需要的库
```python
import requests
from bs4 import BeautifulSoup
import pymongo
```
2. 连接MongoDB数据库
```python
client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["douban_book"]
collection = db["books"]
```
3. 爬取数据并解析
```python
url = "https://book.douban.com/top250"
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"}
res = requests.get(url, headers=headers)
soup = BeautifulSoup(res.text, "html.parser")
```
4. 提取数据并存储到MongoDB中
```python
for book in soup.find_all("tr", class_="item"):
title = book.find("div", class_="pl2").a["title"]
link = book.find("div", class_="pl2").a["href"]
rating = book.find("span", class_="rating_nums").get_text()
author = book.find("p", class_="pl").get_text()
collection.insert_one({"title": title, "link": link, "rating": rating, "author": author})
```
完整代码如下:
```python
import requests
from bs4 import BeautifulSoup
import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["douban_book"]
collection = db["books"]
url = "https://book.douban.com/top250"
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"}
res = requests.get(url, headers=headers)
soup = BeautifulSoup(res.text, "html.parser")
for book in soup.find_all("tr", class_="item"):
title = book.find("div", class_="pl2").a["title"]
link = book.find("div", class_="pl2").a["href"]
rating = book.find("span", class_="rating_nums").get_text()
author = book.find("p", class_="pl").get_text()
collection.insert_one({"title": title, "link": link, "rating": rating, "author": author})
```
注意,如果是第一次使用MongoDB,需要先启动MongoDB服务,可以按照以下步骤进行启动:
1. 在命令行中输入以下命令:
```
mongod --dbpath D:\mongodb\data
```
其中D:\mongodb\data是MongoDB数据存储的路径,可以根据需要进行修改。
2. 启动MongoDB服务后,再运行上述Python代码即可将爬取到的数据存储到MongoDB中。
阅读全文