双色球 Python
时间: 2024-04-27 10:18:08 浏览: 88
双色球是一种彩票游戏,可以使用Python来实现生成双色球号码和抓取双色球开奖号码的功能。下面是两种实现双色球功能的例子:
1. 使用random模块生成随机数来生成双色球号码[^1]:
```python
import random
def generate_lottery_numbers():
red_balls = random.sample(range(1, 34), 6)
red_balls.sort()
blue_ball = random.randint(1, 16)
return red_balls, blue_ball
red_balls, blue_ball = generate_lottery_numbers()
print("Red balls: ", red_balls)
print("Blue ball: ", blue_ball)
```
2. 使用爬虫抓取双色球开奖号码[^2]:
```python
import requests
from bs4 import BeautifulSoup
def get_lottery_numbers():
url = "http://www.example.com" # 替换为实际的双色球开奖网站链接
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
numbers = soup.find("div", class_="lottery-numbers").text.strip()
return numbers
lottery_numbers = get_lottery_numbers()
print("Lottery numbers: ", lottery_numbers)
```
阅读全文