python的应用

用Python写一个双色球选号的游戏并运行它

# 双色球选号游戏
import random
 
# 定义红球数字范围
red_balls = range(1, 34)
 
# 定义蓝球数字范围
blue_balls = range(1, 17)
 
# 生成6个红球
red_balls_number = random.sample(red_balls, 6)
 
# 生成1个蓝球
blue_balls_number = random.sample(blue_balls, 1)
 
# 排序
red_balls_number.sort()
 
# 打印结果
print('您的双色球号码为:' + str(red_balls_number) + ' + ' + str(blue_balls_number))

用Python写一个抓取商品详情页的代码并运行它

import requests
from bs4 import BeautifulSoup
 
# 请求URL
url = 'https://www.jd.com/product/12345.html'
 
# 得到页面的内容
headers={'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36'}
html=requests.get(url,headers=headers,timeout=10).text
 
# 通过content创建BeautifulSoup对象
soup = BeautifulSoup(html, 'lxml')
 
# 获取商品的标题
title = soup.find('div',class_="sku-name").text.strip()
 
# 获取商品的价格
price = soup.find('span',class_="price-type").text.strip()
 
# 获取商品的评论数量
comment = soup.find('span', class_="comment-count").text.strip()
 
# 打印结果
print('商品标题:', title)
print('商品价格:', price)
print('商品评论数:', comment)

了解更多https://o0b.cn/iibill




请使用浏览器的分享功能分享到微信等