python爬取当当网书籍并数据清洗
时间: 2024-09-18 21:09:45 浏览: 103
Python爬取当当网书籍并进行数据清洗通常涉及以下几个步骤:
1. **准备工具**:首先需要安装一些必要的库,如`requests`用于发送HTTP请求获取网页内容,`BeautifulSoup`或`lxml`用于解析HTML,以及数据处理库如`pandas`。
```python
pip install requests beautifulsoup4 pandas
```
2. **发送请求**:通过`requests.get()`函数获取当当网书籍页面的HTML源码。
```python
import requests
url = 'https://book.dangdang.com/' # 示例URL,替换为实际书籍搜索链接
response = requests.get(url)
```
3. **解析HTML**:使用BeautifulSoup解析HTML,提取所需的数据。例如,书籍标题、作者、价格等信息通常位于特定的HTML标签内。
```python
from bs4 import BeautifulSoup
soup = BeautifulSoup(response.text, 'html.parser')
books_info = soup.find_all('div', class_='product-info') # 类似的选择器,找到包含书籍信息的部分
```
4. **数据抽取**:遍历提取到的信息,将每个书籍的相关字段存储在一个列表或字典中,如`title`, `author`, `price`等。
```python
data_list = []
for book in books_info:
title = book.find('h2', class_='product-title').text.strip()
author = book.find('span', class_='author').text.strip() if book.find('span', class_='author') else None
price = book.find('i', class_='price').text.strip() if book.find('i', class_='price') else None
data_list.append({'title': title, 'author': author, 'price': price})
```
5. **数据清洗**:对数据进行预处理,比如去除无关字符、转换数据类型(如果价格是字符串的话)。可能还需要处理缺失值或异常情况。
```python
# 清洗数据
cleaned_data = [dict(item) for item in data_list if all(item.values())] # 只保留数据完整的项
```
6. **保存数据**:最后可以将清洗后的数据保存为CSV或JSON文件,方便后续分析或进一步操作。
```python
import csv
with open('books_cleaned.csv', 'w', newline='', encoding='utf-8') as f:
writer = csv.DictWriter(f, fieldnames=['title', 'author', 'price'])
writer.writeheader()
writer.writerows(cleaned_data)
阅读全文
相关推荐

















