数据采集第二次作业

作业报告

作业一:七日天气预报爬取

主要代码和运行结果:

gitee连接:https://gitee.com/hongjinju/songwenton/tree/master/作业二/天气

代码展示:
from bs4 import BeautifulSoup
from bs4 import UnicodeDammit
import urllib.request
import sqlite3

# 定义一个名为 WeatherDB 的类,用于操作天气数据库
class WeatherDB:
    # 打开数据库的方法
    def openDB(self):
        # 连接到名为 "weathers.db" 的数据库
        self.con = sqlite3.connect("weathers.db")
        # 获取数据库游标
        self.cursor = self.con.cursor()
        try:
            # 尝试创建一个名为 weathers 的表,如果表已存在,则不会执行此操作
            self.cursor.execute("create table weathers (wCity varchar(16),wDate varchar(16),wWeather varchar(64),wTemp varchar(32),constraint pk_weather primary key (wCity,wDate))")
        except:
            # 如果表已存在,删除表中的所有数据
            self.cursor.execute("delete from weathers")

    # 关闭数据库的方法
    def closeDB(self):
        # 提交数据库的更改
        self.con.commit()
        # 关闭数据库连接
        self.con.close()

    # 向数据库插入数据的方法
    def insert(self, city, date, weather, temp):
        try:
            # 执行 SQL 语句,向表中插入数据
            self.cursor.execute("insert into weathers (wCity,wDate,wWeather,wTemp) values (?,?,?,?)", (city, date, weather, temp))
        except Exception as err:
            # 如果插入数据时出现错误,打印错误信息
            print(err)

    # 显示数据库中数据的方法
    def show(self):
        # 执行 SQL 查询语句,获取表中的所有数据
        self.cursor.execute("select * from weathers")
        rows = self.cursor.fetchall()
        # 打印表头
        print("%-16s%-16s%-32s%-16s" % ("city", "date", "weather", "temp"))
        for row in rows:
            # 打印每一行数据
            print("%-16s%-16s%-32s%-16s" % (row[0], row[1], row[2], row[3]))

# 定义一个名为 WeatherForecast 的类,用于获取天气预报信息
class WeatherForecast:
    def __init__(self):
        # 设置请求头,模拟浏览器访问
        self.headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36"}
        # 定义城市代码字典,这里只有北京的代码
        self.cityCode = {"北京": "101010100"}

    # 预测特定城市天气的方法
    def forecastCity(self, city):
        # 如果传入的城市不在城市代码字典中
        if city not in self.cityCode.keys():
            print(city + " code cannot be found")
            return

        # 根据城市代码构建 URL
        url = "http://www.weather.com.cn/weather/" + self.cityCode[city] + ".shtml"
        try:
            # 创建请求对象
            req = urllib.request.Request(url, headers=self.headers)
            # 打开 URL 并读取数据
            data = urllib.request.urlopen(req)
            data = data.read()
            # 尝试将数据转换为 Unicode 编码
            dammit = UnicodeDammit(data, ["utf-8", "gbk"])
            data = dammit.unicode_markup
            # 使用 BeautifulSoup 解析 HTML 数据
            soup = BeautifulSoup(data, "lxml")
            # 选择页面中所有 class 为 't clearfix' 的 ul 标签下的 li 标签
            lis = soup.select("ul[class='t clearfix'] li")
            for li in lis:
                try:
                    # 提取日期、天气和温度信息
                    date = li.select('h1')[0].text
                    weather = li.select('p[class="wea"]')[0].text
                    temp = li.select('p[class="tem"] span')[0].text + "/" + li.select('p[class="tem"] i')[0].text
                    print(city, date, weather, temp)
                    # 将提取到的信息插入数据库
                    self.db.insert(city, date, weather, temp)
                except Exception as err:
                    # 如果在提取信息过程中出现错误,打印错误信息
                    print(err)
        except Exception as err:
            # 如果在请求 URL 或处理数据过程中出现错误,打印错误信息
            print(err)

    # 处理多个城市天气信息的方法
    def process(self, cities):
        # 创建 WeatherDB 对象
        self.db = WeatherDB()
        # 打开数据库
        self.db.openDB()
        for city in cities:
            # 预测每个城市的天气
            self.forecastCity(city)

        #self.db.show()
        # 关闭数据库
        self.db.closeDB()

# 创建 WeatherForecast 对象
ws = WeatherForecast()
# 传入要处理的城市列表,这里只有北京
ws.process(["北京"])
print("completed")

运行结果:

心得:通过这次实践,进一步加深了我对beausoup的使用,同时也让我学会了对数据库的使用

作业二:用requests和BeautifulSoup库方法定向爬取股票相关信息

主要代码和运行结果:

gitee连接:https://gitee.com/hongjinju/songwenton/tree/master/作业二/股票

代码展示:

import requests

def get_js_data(url):
    try:
        response = requests.get(url)
        if response.status_code == 200:
            js_content = response.text
            with open('js_content.txt', 'w', encoding='utf-8') as file:
                file.write(js_content)
            print("JS 内容已保存到 js_content.txt 文件中")
            return js_content
        else:
            print(f"请求失败,状态码:{response.status_code}")
    except requests.exceptions.RequestException as e:
        print(f"请求发生错误:{e}")

url = "https://45.push2.eastmoney.com/api/qt/clist/get?cb=jQuery112409752910203212426_1729039643670&pn=1&pz=20&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&dect=1&wbp2u=|0|0|0|web&fid=f3&fs=b:DLMK0146&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f23,f24,f25,f26,f22,f33,f11,f62,f128,f136,f115,f152&_=1729039643674"
js_data = get_js_data(url)
if js_data:
    print(js_data) #使用api方法爬取网页信息
import json
import sqlite3

# 从文件中读取数据
with open('js_content.txt', 'r', encoding='utf-8') as file:
    content = file.read()

# 提取 JSON 数据部分
start = content.find('{')
end = content.rfind('}') + 1
json_data_str = content[start:end]

# 解析 JSON 数据
data = json.loads(json_data_str)
diff_data = data.get('data', {}).get('diff', [])

# 连接到 SQLite 数据库
conn = sqlite3.connect('stock.db')
cursor = conn.cursor()

# 创建表(如果不存在)
cursor.execute('''
CREATE TABLE IF NOT EXISTS stock_data (
    代码 TEXT,
    名称 TEXT,
    最新价 TEXT,
    涨跌额 TEXT,
    涨跌幅 TEXT,
    今开 TEXT,
    最高 TEXT,
    最低 TEXT,
    成交量 TEXT,
    成交额 TEXT
)
''')

# 插入数据
for item in diff_data:
    f12_value = item.get('f12', '')
    f14_value = item.get('f14', '')
    f2_value = item.get('f2', '')
    f4_value = item.get('f4', '')
    f3_value = item.get('f3', '')
    f17_value = item.get('f17', '')
    f15_value = item.get('f15', '')
    f16_value = item.get('f16', '')
    f5_value = item.get('f5', '')
    f6_value = item.get('f6', '')
    cursor.execute('INSERT INTO stock_data VALUES (?,?,?,?,?,?,?,?,?,?)', 
                   (f12_value, f14_value, f2_value, f4_value, f3_value, f17_value, f15_value, f16_value, f5_value, f6_value))

# 提交事务
conn.commit()

# 关闭连接
conn.close()  #对上一个程序提取的信息进行提取

运行结果:

实验心得:进一步强化我对api的使用和对数据库的使用

作业三:爬取中国大学2021主榜

代码展示和运行结果

代码展示:

gitee连接:https://gitee.com/hongjinju/songwenton/tree/master/作业二/大学排名

import requests

js_url = 'https://www.shanghairanking.cn/_nuxt/static/1728872418/rankings/bcur/2021/payload.js'
response = requests.get(js_url)

with open('downloaded2_js_file.js', 'wb') as file:
    file.write(response.content) #用api和request方法来获取网站的js文件
import re
import sqlite3
with open('downloaded_js_file.js', 'r', encoding='utf-8') as file:
    data = file.read()
rank = re.findall(',ranking:(.*?),',data) #排名
name = re.findall(',univNameCn:"(.*?)",', data) #学校名称
province = re.findall(',province:(.*?),', data)  # 省市
category = re.findall(',univCategory:(.*?),', data)    #学校类型
score = re.findall(',score:(.*?),', data)  # 总分
# 变量字典映射
    #将网页中的映射复制下来,方便映射
  #太长,博客上就不显示了
    # b类字符带有多余空格,直接使用pycharm自带的整体替换,将“ ,”替换为”,“
a2 = "a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, _, $, aa, ab, ac, ad, ae, af, ag, ah, ai, aj, ak, al, am, an, ao, ap, aq, ar, as, at, au, av, aw, ax, ay, az, aA, aB, aC, aD, aE, aF, aG, aH, aI, aJ, aK, aL, aM, aN, aO, aP, aQ, aR, aS, aT, aU, aV, aW, aX, aY, aZ, a_, a$, ba, bb, bc, bd, be, bf, bg, bh, bi, bj, bk, bl, bm, bn, bo, bp, bq, br, bs, bt, bu, bv, bw, bx, by, bz, bA, bB, bC, bD, bE, bF, bG, bH, bI, bJ, bK, bL, bM, bN, bO, bP, bQ, bR, bS, bT, bU, bV, bW, bX, bY, bZ, b_, b$, ca, cb, cc, cd, ce, cf, cg, ch, ci, cj, ck, cl, cm, cn, co, cp, cq, cr, cs, ct, cu, cv, cw, cx, cy, cz, cA, cB, cC, cD, cE, cF, cG, cH, cI, cJ, cK, cL, cM, cN, cO, cP, cQ, cR, cS, cT, cU, cV, cW, cX, cY, cZ, c_, c$, da, db, dc, dd, de, df, dg, dh, di, dj, dk, dl, dm, dn, do0, dp, dq, dr, ds, dt, du, dv, dw, dx, dy, dz, dA, dB, dC, dD, dE, dF, dG, dH, dI, dJ, dK, dL, dM, dN, dO, dP, dQ, dR, dS, dT, dU, dV, dW, dX, dY, dZ, d_, d$, ea, eb, ec, ed, ee, ef, eg, eh, ei, ej, ek, el, em, en, eo, ep, eq, er, es, et, eu, ev, ew, ex, ey, ez, eA, eB, eC, eD, eE, eF, eG, eH, eI, eJ, eK, eL, eM, eN, eO, eP, eQ, eR, eS, eT, eU, eV, eW, eX, eY, eZ, e_, e$, fa, fb, fc, fd, fe, ff, fg, fh, fi, fj, fk, fl, fm, fn, fo, fp, fq, fr, fs, ft, fu, fv, fw, fx, fy, fz, fA, fB, fC, fD, fE, fF, fG, fH, fI, fJ, fK, fL, fM, fN, fO, fP, fQ, fR, fS, fT, fU, fV, fW, fX, fY, fZ, f_, f$, ga, gb, gc, gd, ge, gf, gg, gh, gi, gj, gk, gl, gm, gn, go, gp, gq, gr, gs, gt, gu, gv, gw, gx, gy, gz, gA, gB, gC, gD, gE, gF, gG, gH, gI, gJ, gK, gL, gM, gN, gO, gP, gQ, gR, gS, gT, gU, gV, gW, gX, gY, gZ, g_, g$, ha, hb, hc, hd, he, hf, hg, hh, hi, hj, hk, hl, hm, hn, ho, hp, hq, hr, hs, ht, hu, hv, hw, hx, hy, hz, hA, hB, hC, hD, hE, hF, hG, hH, hI, hJ, hK, hL, hM, hN, hO, hP, hQ, hR, hS, hT, hU, hV, hW, hX, hY, hZ, h_, h$, ia, ib, ic, id, ie, if0, ig, ih, ii, ij, ik, il, im, in0, io, ip, iq, ir, is, it, iu, iv, iw, ix, iy, iz, iA, iB, iC, iD, iE, iF, iG, iH, iI, iJ, iK, iL, iM, iN, iO, iP, iQ, iR, iS, iT, iU, iV, iW, iX, iY, iZ, i_, i$, ja, jb, jc, jd, je, jf, jg, jh, ji, jj, jk, jl, jm, jn, jo, jp, jq, jr, js, jt, ju, jv, jw, jx, jy, jz, jA, jB, jC, jD, jE, jF, jG, jH, jI, jJ, jK, jL, jM, jN, jO, jP, jQ, jR, jS, jT, jU, jV, jW, jX, jY, jZ, j_, j$, ka, kb, kc, kd, ke, kf, kg, kh, ki, kj, kk, kl, km, kn, ko, kp, kq, kr, ks, kt, ku, kv, kw, kx, ky, kz, kA, kB, kC, kD, kE, kF, kG, kH, kI, kJ, kK, kL, kM, kN, kO, kP, kQ, kR, kS, kT, kU, kV, kW, kX, kY, kZ, k_, k$, la, lb, lc, ld, le, lf, lg, lh, li, lj, lk, ll, lm, ln, lo, lp, lq, lr, ls, lt, lu, lv, lw, lx, ly, lz, lA, lB, lC, lD, lE, lF, lG, lH, lI, lJ, lK, lL, lM, lN, lO, lP, lQ, lR, lS, lT, lU, lV, lW, lX, lY, lZ, l_, l$, ma, mb, mc, md, me, mf, mg, mh, mi, mj, mk, ml, mm, mn, mo, mp, mq, mr, ms, mt, mu, mv, mw, mx, my, mz, mA, mB, mC, mD, mE, mF, mG, mH, mI, mJ, mK, mL, mM, mN, mO, mP, mQ, mR, mS, mT, mU, mV, mW, mX, mY, mZ, m_, m$, na, nb, nc, nd, ne, nf, ng, nh, ni, nj, nk, nl, nm, nn, no, np, nq, nr, ns, nt, nu, nv, nw, nx, ny, nz, nA, nB, nC, nD, nE, nF, nG, nH, nI, nJ, nK, nL, nM, nN, nO, nP, nQ, nR, nS, nT, nU, nV, nW, nX, nY, nZ, n_, n$, oa, ob, oc, od, oe, of, og, oh, oi, oj, ok, ol, om, on, oo, op, oq, or, os, ot, ou, ov, ow, ox, oy, oz, oA, oB, oC, oD, oE, oF, oG, oH, oI, oJ, oK, oL, oM, oN, oO, oP, oQ, oR, oS, oT, oU, oV, oW, oX, oY, oZ, o_, o$, pa, pb, pc, pd, pe, pf, pg, ph, pi, pj, pk, pl, pm, pn, po, pp, pq, pr, ps, pt, pu, pv, pw, px, py, pz, pA, pB, pC, pD, pE, pF, pG, pH, pI, pJ"
a3 = a2.split(', ')
b2 = '"", false, null, 0, "理工", "综合", true, "师范", "双一流", "211", "江苏", "985", "农业", "山东", "河南", "河北", "北京", "辽宁", "陕西", "四川", "广东", "湖北", "湖南", "浙江", "安徽", "江西", "黑龙江", "吉林", "上海", "福建", "山西", "云南", "广西", 2, "贵州", "甘肃", "内蒙古", "重庆", "天津", "新疆", 1, "467", "496", "2023-01-05T00:00:00+08:00", "2024,2023,2022,2021,2020", "林业", "5.8", "533", "23.1", "7.3", "海南", "37.9", "28.0", "4.3", "12.1", "16.8", "11.7", "3.7", "4.6", "297", "397", "21.8", "32.2", "16.6", "37.6", "24.6", "13.6", "13.9", "3.3", "5.2", "8.1", "3.9", "5.1", "5.6", "5.4", "2.6", "162", 93.5, 89.4, 11, 14, 10, 13, "宁夏", "青海", "西藏", "11.3", "35.2", "9.5", "35.0", "32.7", "23.7", "33.2", "9.2", "30.6", "8.5", "22.7", "26.3", "8.0", "10.9", "26.0", "3.2", "6.8", "5.7", "13.8", "6.5", "5.5", "5.0", "13.2", "13.3", "15.6", "18.3", "3.0", "21.3", "12.0", "22.8", "3.6", "3.4", "3.5", "95", "109", "117", "129", "138", "147", "159", "185", "191", "193", "196", "213", "232", "237", "240", "267", "275", "301", "309", "314", "318", "332", "334", "339", "341", "354", "365", "371", "378", "384", "388", "403", "416", "418", "420", "423", "430", "438", "444", "449", "452", "457", "461", "465", "474", "477", "485", "487", "491", "501", "508", "513", "518", "522", "528", 83.4, "538", "555", 2021, 7, "12.8", "42.9", "18.8", "36.6", "4.8", "40.0", "37.7", "11.9", "45.2", "31.8", "10.4", "40.3", "11.2", "30.9", "37.8", "16.1", "19.7", "11.1", "23.8", "29.1", "0.2", "24.0", "27.3", "24.9", "39.5", "20.5", "23.4", "9.0", "4.1", "25.6", "12.9", "6.4", "18.0", "24.2", "7.4", "29.7", "26.5", "22.6", "29.9", "28.6", "10.1", "16.2", "19.4", "19.5", "18.6", "27.4", "17.1", "16.0", "27.6", "7.9", "28.7", "19.3", "29.5", "38.2", "8.9", "3.8", "15.7", "13.5", "1.7", "16.9", "33.4", "132.7", "15.2", "8.7", "20.3", "5.3", "0.3", "4.0", "17.4", "2.7", "160", "161", "164", "165", "166", "167", "168", 130.6, 105.5, 4, 2024, 15, "中国大学排名(主榜)", 25, 12, "全部", "1", "88.0", 5, "2", "36.1", "25.9", "3", "34.3", 6, "4", "35.5", "21.6", "39.2", "5", "10.8", "4.9", "30.4", "6", "46.2", "7", "0.8", "42.1", "8", "32.1", "22.9", "31.3", "9", "43.0", "25.7", "10", "34.5", "10.0", "26.2", "46.5", "11", "47.0", "33.5", "35.8", "25.8", "12", "46.7", "13.7", "31.4", "33.3", "13", "34.8", "42.3", "13.4", "29.4", "14", "30.7", "15", "42.6", "26.7", "16", "12.5", "17", "12.4", "44.5", "44.8", "18", "10.3", "15.8", "19", "32.3", "19.2", "20", "21", "28.8", "9.6", "22", "45.0", "23", "30.8", "16.7", "16.3", "24", "25", "32.4", "26", "9.4", "27", "33.7", "18.5", "21.9", "28", "30.2", "31.0", "16.4", "29", "34.4", "41.2", "2.9", "30", "38.4", "6.6", "31", "4.4", "17.0", "32", "26.4", "33", "6.1", "34", "38.8", "17.7", "35", "36", "38.1", "11.5", "14.9", "37", "14.3", "18.9", "38", "13.0", "39", "27.8", "33.8", "3.1", "40", "41", "28.9", "42", "28.5", "38.0", "34.0", "1.5", "43", "15.1", "44", "31.2", "120.0", "14.4", "45", "149.8", "7.5", "46", "47", "38.6", "48", "49", "25.2", "50", "19.8", "51", "5.9", "6.7", "52", "4.2", "53", "1.6", "54", "55", "20.0", "56", "39.8", "18.1", "57", "35.6", "58", "10.5", "14.1", "59", "8.2", "60", "140.8", "12.6", "61", "62", "17.6", "63", "64", "1.1", "65", "20.9", "66", "67", "68", "2.1", "69", "123.9", "27.1", "70", "25.5", "37.4", "71", "72", "73", "74", "75", "76", "27.9", "7.0", "77", "78", "79", "80", "81", "82", "83", "84", "1.4", "85", "86", "87", "88", "89", "90", "91", "92", "93", "109.0", "94", 235.7, "97", "98", "99", "100", "101", "102", "103", "104", "105", "106", "107", "108", 223.8, "111", "112", "113", "114", "115", "116", 215.5, "119", "120", "121", "122", "123", "124", "125", "126", "127", "128", 206.7, "131", "132", "133", "134", "135", "136", "137", 201, "140", "141", "142", "143", "144", "145", "146", 194.6, "149", "150", "151", "152", "153", "154", "155", "156", "157", "158", 183.3, "169", "170", "171", "172", "173", "174", "175", "176", "177", "178", "179", "180", "181", "182", "183", "184", 169.6, "187", "188", "189", "190", 168.1, 167, "195", 165.5, "198", "199", "200", "201", "202", "203", "204", "205", "206", "207", "208", "209", "210", "212", 160.5, "215", "216", "217", "218", "219", "220", "221", "222", "223", "224", "225", "226", "227", "228", "229", "230", "231", 153.3, "234", "235", "236", 150.8, "239", 149.9, "242", "243", "244", "245", "246", "247", "248", "249", "250", "251", "252", "253", "254", "255", "256", "257", "258", "259", "260", "261", "262", "263", "264", "265", "266", 139.7, "269", "270", "271", "272", "273", "274", 137, "277", "278", "279", "280", "281", "282", "283", "284", "285", "286", "287", "288", "289", "290", "291", "292", "293", "294", "295", "296", "300", 130.2, "303", "304", "305", "306", "307", "308", 128.4, "311", "312", "313", 125.9, "316", "317", 124.9, "320", "321", "Wuyi University", "322", "323", "324", "325", "326", "327", "328", "329", "330", "331", 120.9, 120.8, "Taizhou University", "336", "337", "338", 119.9, 119.7, "343", "344", "345", "346", "347", "348", "349", "350", "351", "352", "353", 115.4, "356", "357", "358", "359", "360", "361", "362", "363", "364", 112.6, "367", "368", "369", "370", 111, "373", "374", "375", "376", "377", 109.4, "380", "381", "382", "383", 107.6, "386", "387", 107.1, "390", "391", "392", "393", "394", "395", "396", "400", "401", "402", 104.7, "405", "406", "407", "408", "409", "410", "411", "412", "413", "414", "415", 101.2, 101.1, 100.9, "422", 100.3, "425", "426", "427", "428", "429", 99, "432", "433", "434", "435", "436", "437", 97.6, "440", "441", "442", "443", 96.5, "446", "447", "448", 95.8, "451", 95.2, "454", "455", "456", 94.8, "459", "460", 94.3, "463", "464", 93.6, "472", "473", 92.3, "476", 91.7, "479", "480", "481", "482", "483", "484", 90.7, 90.6, "489", "490", 90.2, "493", "494", "495", 89.3, "503", "504", "505", "506", "507", 87.4, "510", "511", "512", 86.8, "515", "516", "517", 86.2, "520", "521", 85.8, "524", "525", "526", "527", 84.6, "530", "531", "532", "537", 82.8, "540", "541", "542", "543", "544", "545", "546", "547", "548", "549", "550", "551", "552", "553", "554", 78.1, "557", "558", "559", "560", "561", "562", "563", "564", "565", "566", "567", "568", "569", "570", "571", "572", "573", "574", "575", "576", "577", "578", "579", "580", "581", "582", 9, "2024-04-18T00:00:00+08:00", "logo\u002Fannual\u002Fbcur\u002F2024.png", "软科中国大学排名于2015年首次发布,多年来以专业、客观、透明的优势赢得了高等教育领域内外的广泛关注和认可,已经成为具有重要社会影响力和权威参考价值的中国大学排名领先品牌。软科中国大学排名以服务中国高等教育发展和进步为导向,采用数百项指标变量对中国大学进行全方位、分类别、监测式评价,向学生、家长和全社会提供及时、可靠、丰富的中国高校可比信息。", "学生、家长、高校管理人员、高教研究人员等", 2023, 2022, 2020, 2019, 2018, 2017, 2016, 2015, "logo\u002FindAnalysis\u002Fbcur.png", "中国大学排名", "国内", "大学"'
b3 = b2.split(', ')
a4="a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,_,$,aa,ab,ac,ad,ae,af,ag,ah,ai,aj,ak,al,am,an,ao,ap,aq,ar,as,at,au,av,aw,ax,ay,az,aA,aB,aC,aD,aE,aF,aG,aH,aI,aJ,aK,aL,aM,aN,aO,aP,aQ,aR,aS,aT,aU,aV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nC,nD,nE,nF,nG,nH,nI,nJ,nK,nL,nM,nN,nO,nP,nQ,nR,nS,nT,nU,nV,nW,nX,nY,nZ,n_,n$,oa,ob,oc,od,oe,of,og,oh,oi,oj,ok,ol,om,on,oo,op,oq,or,os,ot,ou,ov,ow,ox,oy,oz,oA,oB,oC,oD,oE,oF,oG,oH,oI,oJ,oK,oL,oM,oN,oO,oP,oQ,oR,oS,oT,oU,oV,oW,oX,oY,oZ,o_,o$,pa,pb,pc,pd,pe,pf,pg,ph,pi,pj,pk,pl,pm,pn,po,pp,pq,pr,ps,pt,pu,pv,pw,px,py,pz,pA,pB,pC,pD,pE,pF,pG,pH,pI,pJ"

a5=a4.split(',')
code_tmp = dict(zip(a5,b3))
for i in range(len(name)):
        rank[i] = code_tmp[rank[i]]
        province[i] = code_tmp[province[i]]
        category[i] = code_tmp[category[i]]
        #有部分score数据带有编码,有些没有
        try:
            score[i]=float(score[i])
        except:
            score[i]=float(code_tmp[score[i]])






# 创建数据库连接
conn = sqlite3.connect('schoolranking.db')

# 创建游标
cursor = conn.cursor()

# 创建表
cursor.execute('''
    CREATE TABLE IF NOT EXISTS schoolranking (
        校名 TEXT,
        排名 INTEGER,
        省份 TEXT,
        类别 TEXT
    )
''')

# 插入数据
for j in range(len(province)):
    cursor.execute("INSERT INTO schoolranking (校名, 排名, 省份, 类别) VALUES (?,?,?,?)",
                   (name[j], rank[j], province[j], category[j]))

# 提交更改
conn.commit()

# 关闭连接
conn.close() #再对爬取来的js文件进行提取。其中js文件含有function函数,即变量间的一一映射关系,因此初步提取的信息并不完全正确,需要利用function进行映射调整。这里采用构建字典的方法

运行结果:


实验心得:学会了对js文件的特色处理

posted @ 2024-10-25 21:43  关忆南北  阅读(41)  评论(0编辑  收藏  举报