读取的豆瓣图书Top250是数据完整且格式统一的,用python代码挑选出所有评分一样的书籍中价格便宜 的书籍,并将它们打印出来。
时间: 2024-06-11 09:06:45 浏览: 106
抱歉,作为AI语言模型,我无法直接获取豆瓣图书Top250的数据。不过,我可以提供一些大概的思路和代码示例,供您参考:
1. 使用Python的Requests库和BeautifulSoup库来爬取豆瓣图书Top250的网页数据。
2. 解析网页数据,提取出书籍的评分和价格信息,并将它们存储在一个Python字典中。
3. 遍历所有的书籍,找出评分相同的书籍,并在这些书籍中找出价格最低的书籍。
4. 将价格最低的书籍打印出来。
下面是一些示例代码,供您参考:
```
import requests
from bs4 import BeautifulSoup
# 爬取豆瓣图书Top250的网页数据
url = 'https://book.douban.com/top250'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# 解析网页数据,提取出书籍的评分和价格信息
books = []
for book in soup.select('tr.item'):
title = book.select_one('.pl2 a')['title']
rating = float(book.select_one('.rating_num').text)
price = float(book.select_one('.pl span').text.strip('元'))
books.append({'title': title, 'rating': rating, 'price': price})
# 找出评分相同的书籍,并在这些书籍中找出价格最低的书籍
same_rating_books = {}
for book in books:
if book['rating'] not in same_rating_books:
same_rating_books[book['rating']] = [book]
else:
same_rating_books[book['rating']].append(book)
lowest_price_books = []
for rating, books in same_rating_books.items():
lowest_price = min(books, key=lambda x: x['price'])['price']
lowest_price_books.extend([book for book in books if book['price'] == lowest_price])
# 打印出价格最低的书籍
for book in lowest_price_books:
print(book['title'], book['rating'], book['price'])
```
阅读全文