BeautifulSoup 网页中 table 解析范例
python 2.7 编写 soupTable.py 如下
# -*- coding: cp936 -*-
import urllib
import urllib2
import os, re
from BeautifulSoup import BeautifulSoup
URL = "?"
page = urllib2.urlopen(URL)
soup = BeautifulSoup(page)
page.close()
tables = soup.findAll('table')
tab = tables[0]
for tr in tab.findAll('tr'):
for td in tr.findAll('td'):
print td.getText(),
print
#
python 3.7+ : pip install beautilfulsoup4 或者 pip install bs4
编写 get_2seq.py 如下
# -*- coding: utf-8 -*-
import os,sys
import requests
from bs4 import BeautifulSoup
# 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