用python生成一个爬虫程序每天从网上获取排列5自第一期至最近一期开奖号将获取的数据生成excel文件保存。要求:程序不使用库
因为爬取数据需要访问网站并解析HTML代码,所以必须使用一些库来实现。以下是使用Python内置库进行爬取和数据处理的示例代码:
import requests
import re
import datetime
# 获取当前日期
today = datetime.date.today().strftime('%Y%m%d')
# 构造请求URL
url = f'http://www.cwl.gov.cn/cwl_admin/kjxx/findDrawNotice?name=pl5&issueCount=100&issueStart={today}&issueEnd={today}'
# 发送请求并获取响应内容
response = requests.get(url)
html = response.text
# 使用正则表达式解析HTML代码,获取开奖号码
pattern = re.compile(r'"result":\["(\d+)",(\d+),(\d+),(\d+),(\d+)')
result = pattern.findall(html)
# 生成Excel文件
filename = f'pl5_{today}.xlsx'
with open(filename, 'w') as f:
f.write('期号,号码1,号码2,号码3,号码4,号码5\n')
for r in result:
f.write(','.join(r) + '\n')
注意:这个示例程序中,Excel文件是使用文本模式创建的,如果需要生成真正的Excel文件,需要使用第三方库,例如openpyxl
原文地址: https://www.cveoy.top/t/topic/dqVU 著作权归作者所有。请勿转载和采集!