python 2.7 : 编写 get_pl5.py 如下
# -*- coding: utf-8 -*-
import os,sys
import urllib
import urllib2
from BeautifulSoup import BeautifulSoup
# 体彩 排列5
URL = "http://www.lottery.gov.cn/historykj/history.jspx?_ltype=plw"
page = urllib2.urlopen(URL)
soup = BeautifulSoup(page)
page.close()
fp = open("pl5.txt","w")
tables = soup.findAll('table')
tab = tables[0]
for tr in tab.tbody.findAll('tr'):
for td in tr.findAll('td'):
text = td.getText().encode('cp936')+'!'
fp.write(text)
fp.write('\n')
#
fp.close()
python 3.7+ : pip install beautilfulsoup4 或者 pip install bs4
# -*- coding: utf-8 -*-
import os,sys
import requests
from bs4 import BeautifulSoup
# urllib.request.urlopen
# main()
if len(sys.argv) ==2:
n = sys.argv[1]
else:
print('usage: get_2seq.py n ')
sys.exit(1)
n = '5'
# 福彩 双色球
url = "https://www.gdfc.org.cn/play_list_game_"+ n +".html"
header ={"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/95.0.4638.69 "}
resp = requests.get(url, headers=header)
resp.encoding = resp.apparent_encoding
soup = BeautifulSoup(resp.text, "html.parser")
resp.close()
fp = open("fc2seq.txt", 'w', encoding='utf-8')
tables = soup.find_all('table')
tab = tables[0]
for tr in tab.find_all('tr')[1:]:
for td in tr.find_all('td')[:-1]:
dict1 = td.attrs
txt =''
if 'luckyno' in dict1:
t = dict1['luckyno']
if len(t) ==14:
txt = ','.join((t[0:2],t[2:4],t[4:6],t[6:8],t[8:10],t[10:12]))+'!'+t[12:]
else:
txt = t
else:
txt = td.text
fp.write(txt+'!')
fp.write('\n')
fp.close()
运行 python get_2seq.py 5