请检查以下代码,如果有问题,请告诉我。
import requests
from bs4 import BeautifulSoup
import pprint
page = requests.get("https://www.4dpredict.com/mysingaporetoto.p3.html")
html = BeautifulSoup(page.content, 'html.parser')
tables = html.find_all('table')
table_data = dict()
for table_id, table in enumerate(tables):
print('[!] Scraping Table -', table_id + 1)
table_data['table_{}'.format(table_id+1)] = dict()
table_info = table_data['table_{}'.format(table_id+1)]
for row_id, row in enumerate(table.find_all('tr')):
col = []
for val in row.find_all('td'):
val = val.text
val = val.replace('\n', '').strip()
if val:
col.append(val)
table_info['row_{}'.format(row_id+1)] = col
pprint.pprint(table_info)
print('+-+' * 20)
pprint.pprint(table_data)
样例输出
[!] Scraping Table - 1
{'row_1': ['SINGAPORE TOTO2018-08-23 (Thu) 3399'],
'row_10': ['Group 2', '$', '-'],
'row_11': ['Group 3', '$1,614', '124'],
'row_12': ['Group 4', '$344', '318'],
'row_13': ['Group 5', '$50', '6,876'],
'row_14': ['Group 6', '$25', '9,092'],
'row_15': ['Group 7', '$10', '117,080'],
'row_16': ['SHOW ANALYSISEVEN : ODD, 2 : 5SUM :138, AVERAGE :23 MIN :02, MAX '
':41, DIFF :39',
'EVEN : ODD, 2 : 5',
'SUM :138, AVERAGE :23',
'MIN :02, MAX :41, DIFF :39'],
'row_17': ['EVEN : ODD, 2 : 5'],
'row_18': ['SUM :138, AVERAGE :23'],
'row_19': ['MIN :02, MAX :41, DIFF :39'],
'row_2': ['WINNING NUMBERS'],
'row_3': ['02', '03', '23', '30', '39', '41'],
'row_4': ['ADDITIONAL'],
'row_5': ['19'],
'row_6': ['Prize: $2,499,788'],
'row_7': ['WINNING SHARES'],
'row_8': ['Group', 'Share Amt', 'Winners'],
'row_9': ['Group 1', '$1,249,894', '2']}
+-++-++-++-++-++-++-++-++-++-++-++-++-++-++-++-++-++-++-++-+
['%s'%num]
替换为[f'{num}']
。 - SIM