国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

將網(wǎng)頁(yè)的解析數(shù)據(jù)存儲(chǔ)為html文件

2019-11-11 07:21:55
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

為了把從網(wǎng)頁(yè)中解析的數(shù)據(jù)存儲(chǔ)起來(lái),便于存檔和查詢(xún),可以把數(shù)據(jù)存放在數(shù)據(jù)庫(kù)中,也可以以文件的方式存儲(chǔ),還可以存儲(chǔ)為網(wǎng)絡(luò)應(yīng)用程序,即html文件或者CSV格式文件。本文引用一個(gè)例子,看看如何將網(wǎng)頁(yè)的解析數(shù)據(jù)存儲(chǔ)為html文件。

from bs4 import BeautifulSoupimport requests#前置標(biāo)簽PRe_html = '''<!DOCTYPE HTML><html><head><!--meta charset = 'utf-8'--><title>油價(jià)歷史數(shù)據(jù)</title></head><body><h2>自去年11月份以來(lái)的油價(jià)數(shù)據(jù)(取自本例網(wǎng)站)</h2><table width=600 border=1><tr><td>日期</td><td>92#無(wú)鉛</td><td>95#無(wú)鉛</td><td>98#無(wú)鉛</td></tr>'''#后置標(biāo)簽post_html = """</table></body></html>"""url = 'http://new.cpc.com.tw/division/mb/oil-more4.aspx'src = requests.get(url)#src.encoding = "bgk"html = src.textbs = BeautifulSoup(html, 'html.parser')data = bs.find_all('span' ,{'id':'Showtd'} )rows = data[0].find_all('tr')prices = list()i = 0for row in rows:    if i > 18:break;    cols = row.find_all("td")    if len(cols[1].text ) > 0:        item = [cols[0].text, cols[1].text, cols[2].text, cols[3].text]        prices.append(item)    i += 1#生成數(shù)據(jù)表html_body = ''for p in prices:    html_body += "<tr><td>{}</td><td>{}</td><td>{}</td><td>{}</td></tr>".format(p[0],p[1],p[2],p[3])html_file = pre_html + html_body + post_html#按文本文件的方式生成html文件fp = open('oilprice.html','w')fp.write(html_file)fp.close()

用瀏覽器打開(kāi)執(zhí)行的結(jié)果:


發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 古浪县| 施甸县| 衡山县| 永仁县| 广西| 葫芦岛市| 汝南县| 包头市| 扬州市| 望奎县| 平远县| 玉屏| 建德市| 杨浦区| 永州市| 区。| 龙川县| 连南| 界首市| 海丰县| 商洛市| 朝阳县| 鲁甸县| 平顶山市| 鲜城| 仪征市| 鸡西市| 汉寿县| 巴林右旗| 罗甸县| 襄城县| 兴宁市| 浠水县| 衡阳市| 龙里县| 新乡市| 教育| 永泰县| 枣阳市| 五家渠市| 博兴县|