【问题标题】:Python web scraping exchange pricesPython网页抓取交易价格
【发布时间】:2017-11-08 23:34:45
【问题描述】:

我想从这个网站上抓取交易所价格信息,然后把它存入数据库:https://www.mnb.hu/arfolyamok

我写了这段代码,但它有问题。我该如何解决它,我必须在哪里更改它? 我正在 Windows 7 上使用 Python 2.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)

【问题讨论】:

  • 您面临的实际问题是什么?
  • 这是您遇到的错误吗? SyntaxError: Non-ASCII character '\xc3' in file scrapetest.py on line 24, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
  • 我们不是代码编写服务,这里的问题应该有一个明确的问题,应该列出您尝试解决的问题,并且应该包含任何信息可以帮助我们找出问题所在。你说something wrong with it,但你根本没有说明它出了什么问题。我们需要知道您的预期结果是什么,以及您实际得到的结果是什么。请查看How to ask a good question

标签: python html database web-scraping screen-scraping


【解决方案1】:

# coding=utf-8 添加到代码顶部。这将有助于解决您收到的 SyntaxError。还要确保你的缩进是正确的!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-05
    • 1970-01-01
    • 1970-01-01
    • 2017-09-11
    • 1970-01-01
    • 2022-01-08
    相关资源
    最近更新 更多