【原文链接】Python代码集----获取最近100期双色球的开奖结果并将结果写入csv文件
(1)首先需要安装依赖包
pip install requestspip install beautifulsoup4pip install lxml
(2)代码实现如下,比如保存为main.py文件
import csvimport requestsfrom bs4 import BeautifulSoupdef main():url="/ssq/history/newinc/history.php?limit=100&sort=0"headers={"user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36"}rs=requests.get(url=url,headers=headers)datas=[]soup = BeautifulSoup(rs.text, 'lxml')for elem in soup.find_all(name="tr",attrs={"class":"t_tr1"}):data=[]for elem2 in elem.find_all(name="td"):txt=elem2.text.strip()if txt:data.append(txt)for i in [8,10,12,13]:data[i]=int(data[i].replace('"',"").replace(",",""))datas.append(data)with open("recent_100_results.csv","w+",encoding="utf8",newline='') as f:csv_writer = csv.writer(f)csv_writer.writerows(datas)if __name__=="__main__":main()
(3)执行python main.py,执行完成后会在当前目录下生成recent_100_results.csv 文件,为生成的结果文件,类似如下图所示
如果觉得《Python代码集----获取最近100期双色球的开奖结果并将结果写入csv文件》对你有帮助,请点赞、收藏,并留下你的观点哦!