写一段爬虫程序爬取双色球往期开奖号码
时间: 2023-06-11 20:09:57 浏览: 197
好的,以下是一个简单的 Python 爬虫程序,用于爬取中国彩票双色球往期开奖号码:
```python
import requests
from bs4 import BeautifulSoup
url = 'http://datachart.500.com/ssq/history/newinc/history.php?start=01001&end=21116'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
table = soup.find('table', attrs={'class': 't1'})
rows = table.tbody.find_all('tr')
for row in rows:
data = row.find_all('td')
if len(data) == 0:
continue
issue_num = data[0].text.strip() # 期号
red_balls = [data[i].text.strip() for i in range(1, 7)] # 红球
blue_ball = data[7].text.strip() # 蓝球
print(issue_num, red_balls, blue_ball)
```
这个程序使用 requests 库发送 HTTP 请求,并使用 BeautifulSoup 库解析 HTML 页面。我们找到表格元素并遍历每一行,提取期号、红球和蓝球,最后打印输出。这个程序可以爬取从 2001 年到现在的所有双色球开奖号码。