UnicodeDecodeError:'utf-8'编解码器无法解码位置0的字节0xfc:无效的起始字节

时间:2019-09-02 22:56:21

标签: python csv beautifulsoup

您好,我正在为一家博彩公司筹集资金,以获取下一支即将参加比赛的球队的名字(我想刮擦德国甲级联赛的名字)。我想将名称的字符串保存在csv文件中,但即时通讯出现错误。当我打开.csv文件时,其内容是:

70,111,114,116,117,110,97,32,68,195,188,115,115,101,108,100,111,114,102

它是“ FortunaDüsseldorf”的ascii代码

我已经在第27行和其他一些代码中尝试了encode(“ utf-8”),但仍然收到错误。第18行不是问题,因为该程序在.csv文件中写入了团队的名字。我不会为第二个团队名称粘贴第二个循环,因为它是相同的代码,但在html中为第二个团队。

import requests
from bs4 import BeautifulSoup
import pandas
import os
import csv

class NamenCollector:


    def NamenHolen(self, url, pfad):
        bool1 = False
        bool2 = False
        self.page = requests.get(url)
        self.soup = BeautifulSoup(self.page.content, 'lxml')
        Namen1 = self.soup.find_all('div', class_='live_group1')
        Namen2 = self.soup.find_all('div', class_='live_group2')
        if os.stat(pfad).st_size == 0:
            nam = self.soup.find('div', class_='live_group1').string.strip().encode("utf-8")
            with open(pfad, 'a') as csvFile:
                writer = csv.writer(csvFile)
                writer.writerow(nam)
                csvFile.close()

        if os.stat(pfad).st_size != 0:
            a1 = pandas.read_csv(pfad)
            for name1 in Namen2:
                nam1 = name1.find('div', class_='live_group1').string.strip().encode("utf-8")
                for name1_1 in a1:
                    if nam1 == name1_1:
                        bool1 = True
                if bool1 == False:
                    with open(pfad, 'a') as csvFile:
                        writer = csv.writer(csvFile)
                        writer.writerow(nam1)
                        csvFile.close()
                        bool1 = False
bun_url = 
'https://www.cashpoint.de/de/bets/bets.html?group=683883sportid=1&filter='

bun_pfad = 'C:\Man\Deutschland\Bundesliga.csv'

d_Bundesliga = NamenCollector()

d_Bundesliga.NamenHolen(bun_url, bun_pfad)   

错误是:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfc in position 0: invalid start byte

0 个答案:

没有答案