首页 > 解决方案 > linux中的Python unicode错误,但不是windows

问题描述

我按照一些指南拼凑了这段python

import requests
import sys
from bs4 import BeautifulSoup

url = requests.get(sys.argv[1])

html = BeautifulSoup(url.content,'html.parser')

for br in html.find_all("br"):
    br.replace_with(" ")

for tr in html.find_all('tr'):
    data = []   

    for td in tr.find_all('td'):
        data.append(td.text.strip())

    if data:
        print("{}".format(','.join(data)))

在 Windows 中,它按我的预期工作。

在Linux中我得到

Traceback (most recent call last):
  File "html2csv.py", line 19, in <module>
    print("{}".format(','.join(data)))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xb0' in position 4: ordinal not in range(128)

我需要在我的脚本中进行哪些更改以防止这种情况发生?我读到您可以忽略问题字符,但有人说这不是正确的方法吗?不知道如何将我找到的任何解决方案实施到我所拥有的中。

标签: pythonlinux

解决方案


很抱歉浪费您的时间。

我在用...

python script.py

默认为 2.7

我需要运行的是...

python3 script.py

推荐阅读