编辑代码

# coding:utf-8
#JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。 
import re
import urllib.request as  request


import json


def gethtmldate():
    url = "http://www.weather.com.cn/static/html/legend.shtml"
    res = request.urlopen(url, None, 2)
    html = res.read().decode('utf-8')
    soup = BeautifulSoup(html, 'html.parser')
    table = soup.select('table')
    result = []
    for i in range(0, len(table)):
        if i > 0 & i < 2:
            res = []
            keys = []
            values = []
            imgs = table[i].select('a>img')
            spans = table[i].select('td>p>a')
            for j in range(0, len(spans)):
                key = ''
                value = ''
                if j % 2 == 0:
                    img = spans[j].select('a>img')
                    value = img[0]['src']
                    values.append(img[0]['src'])
                else:
                    a = spans[j]
                    key = a.contents[0]
                    keys.append(a.contents[0])
            res = dict(zip(keys, values))
            result.append(res)

    filename = 'names.json'
    with open(filename, 'w', encoding='utf-8') as file_obj:
        json.dump(result, file_obj, ensure_ascii=False)
    print(result)
    return html


gethtmldate()