用beautifulsoup爬取豆瓣读书Top250-保存为表格、
时间: 2023-06-08 11:04:15 浏览: 327
Python爬虫实例-爬取豆瓣Top250-保存为表格
5星 · 资源好评率100%
好的,我可以为您提供帮助。下面是代码示例:
```python
import requests
from bs4 import BeautifulSoup
import csv
url = 'https://book.douban.com/top250'
# 发送请求
response = requests.get(url)
content = response.text
soup = BeautifulSoup(content, 'html.parser')
table_head = ['排名', '书名', '评分', '评价人数', '作者', '出版社', '出版日期', '价格']
# 创建表格并写入表头
with open('豆瓣读书Top250.csv', mode='w', encoding='utf-8', newline='') as file:
writer = csv.writer(file)
writer.writerow(table_head)
# 爬取书籍信息
books = soup.select('.pl2 a')
for index, book in enumerate(books):
book_title = book.get_text(strip=True)
book_url = book['href']
book_info = book.parent.parent.select('.rating-num, .pl')[0].get_text(strip=True).split('/')
book_rating = book_info[0]
book_comments = book_info[1][:-3]
book_author, book_publisher, *book_attr, book_price = book.parent.parent.select('.pl')[0].get_text(strip=True).split('/')
# 将书籍信息写入表格
writer.writerow([index+1, book_title, book_rating, book_comments, book_author, book_publisher, book_attr[0], book_price])
print('数据写入完毕!')
```
希望对您有所帮助!如果您还有其他问题,请继续提出。
阅读全文