爬取豆瓣读书网中的解析数据代码
时间: 2023-07-15 15:13:20 浏览: 49
以下是使用Python和BeautifulSoup库爬取豆瓣读书网中的解析数据的代码示例:
```python
import requests
from bs4 import BeautifulSoup
# 要爬取的页面URL
url = 'https://book.douban.com/top250'
# 发送请求并获取HTML内容
response = requests.get(url)
html = response.content
# 使用BeautifulSoup解析HTML内容
soup = BeautifulSoup(html, 'html.parser')
# 获取书籍列表
book_list = soup.find_all('div', {'class': 'pl2'})
# 遍历书籍列表并输出书名和作者
for book in book_list:
title = book.find('a')['title']
author = book.find('p', {'class': 'pl'}).text
print(f'{title} - {author}')
```
以上代码会输出豆瓣读书榜单中前250本书的书名和作者。你可以根据自己的需求修改代码,并添加相应的数据处理和存储功能。
相关问题
用python爬取豆瓣读书网有500兆数据的代码
请注意,爬取豆瓣的数据有可能违反豆瓣的使用条款,建议先查看豆瓣的规定并获得授权。以下是一个示例代码:
```python
import requests
import time
import json
import os
# 设置请求头
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'}
# 设置保存数据的文件夹路径
folder_path = './douban_book_data/'
if not os.path.exists(folder_path):
os.makedirs(folder_path)
# 设置要爬取的页数
page_num = 500
for i in range(0, page_num):
# 构造请求url
url = 'https://m.douban.com/rexxar/api/v2/subject_collection/book_nonfiction/items?os=android&for_mobile=1&start={}&count=20&loc_id=0&_=1616666626618'.format(
i * 20)
# 发送请求
response = requests.get(url, headers=headers)
time.sleep(1) # 为了防止被封,设置请求间隔为1秒
# 解析响应
data = json.loads(response.text)
# 保存数据到文件
file_path = folder_path + 'douban_book_data_{}.txt'.format(i)
with open(file_path, 'w', encoding='utf-8') as f:
f.write(json.dumps(data, ensure_ascii=False))
print('成功保存第{}页数据到文件'.format(i))
```
解释:以上代码使用 requests 库发送 GET 请求,请求豆瓣读书网的非虚构类图书数据。每次请求20条数据,循环请求多页数据,并将每页数据保存到本地文件中。需要注意的是,为了防止被封,代码中设置了1秒的请求间隔。
python爬取豆瓣读书网存在mongodb中
好的,首先需要安装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中。