以下是爬取双色球每期开奖号码的Python代码:

import requests
from bs4 import BeautifulSoup

url = 'http://datachart.500.com/ssq/history/newinc/history.php'

# 构造请求头部信息
header = {
    '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(url, headers=header)
response.encoding = 'utf-8'

# 解析HTML文档
soup = BeautifulSoup(response.text, 'html.parser')
table = soup.find('table', attrs={'class': 'tablelist'})

# 遍历表格获取每期开奖号码
for tr in table.find_all('tr'):
    td_list = tr.find_all('td')
    if len(td_list) == 0:
        continue
    period = td_list[0].text.strip()
    red_ball = [td.text for td in td_list[1:7]]
    blue_ball = td_list[7].text.strip()
    print('期号:{},红球:{},蓝球:{}'.format(period, ' '.join(red_ball), blue_ball))

代码中首先通过requests库发送请求并获取响应,然后使用BeautifulSoup库解析HTML文档。通过寻找表格元素,遍历每一行获取每期开奖号码,包括期号、红球号码和蓝球号码,并输出到控制台。

Python爬取双色球历史开奖号码代码示例

原文地址: https://www.cveoy.top/t/topic/lUvZ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录