python代码 1.制作一个比价系统。 2.比价结果网页中展示出来,通过饼图形式展示。
时间: 2024-04-28 12:25:56 浏览: 10
好的,针对你的要求,我可以提供以下的 Python 代码:
```
import requests
from bs4 import BeautifulSoup
import matplotlib.pyplot as plt
# 获取商品价格信息
def get_product_price(product_url):
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'}
response = requests.get(product_url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
price = soup.find('span', attrs={'class': 'price'}).text.strip()
return float(price[1:])
# 比价系统
def compare_prices(product_urls):
prices = []
for url in product_urls:
prices.append(get_product_price(url))
return prices
# 饼图展示比价结果
def show_price_comparison(prices, labels):
fig, ax = plt.subplots()
ax.pie(prices, labels=labels, autopct='%1.1f%%', startangle=90)
ax.axis('equal')
plt.show()
# 调用比价系统和饼图展示函数
product_urls = ['http://www.example.com/product1', 'http://www.example.com/product2', 'http://www.example.com/product3']
prices = compare_prices(product_urls)
labels = ['Product 1', 'Product 2', 'Product 3']
show_price_comparison(prices, labels)
```
这段代码实现了一个简单的比价系统,通过输入商品的 URL,获取商品价格,然后展示出比价结果的饼图。你可以根据自己的需求修改商品的 URL,或者对饼图的展示方式进行调整。