Python网刮交换价格

2024-04-26 07:54:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从这个网站上刮下外汇价格信息,然后把它放到数据库:https://www.mnb.hu/arfolyamok

我写了这个代码,但有点不对劲。我该怎么修,在哪里改? 我正在Windows7上使用Python2.7.13。你知道吗

代码如下:

import csv import requests from BeautifulSoup import BeautifulSoup url = 'https://www.mnb.hu/arfolyamok' response = requests.get(url) html = response.content soup = BeautifulSoup(html) table = soup.find('tbody', attrs={'class': 'stripe'}) list_of_rows = [] for row in table.findAll('tr')[1:]: list_of_cells = [] for cell in row.findAll('td'): text = cell.text.replace(' ', '') list_of_cells.append(text) list_of_rows.append(list_of_cells) print list_of_rows outfile = open("./inmates.csv", "wb") writer = csv.writer(outfile) writer.writerow(["Pénznem", "Devizanév", "Egység", "Forintban kifejezett érték"]) writer.writerows(list_of_rows)

Tags: ofcsv代码texthttpsimportwwwlist