Python-UnicodeCodeError:';charmap';编解码器可以';t对229393位置的字节0x81进行解码::字符映射到<;未定义>;

Python-UnicodeCodeError:';charmap';编解码器可以';t对229393位置的字节0x81进行解码::字符映射到<;未定义>;,python,python-3.x,selenium,web-scraping,Python,Python 3.x,Selenium,Web Scraping,我曾尝试使用Python和Selenium来创建一个网站 以下是部分代码: def data_html_text(self): #Downloads page source code Xyz_page_source = self.driver.page_source with open(self.Html_source, 'w', encoding="utf-8") as file: file.write(Xy

我曾尝试使用Python和Selenium来创建一个网站

以下是部分代码:

    def data_html_text(self): #Downloads page source code
        Xyz_page_source = self.driver.page_source
        with open(self.Html_source, 'w', encoding="utf-8") as file:
             file.write(Xyz_page_source)


    def email_parser(self): # gets scraped links and filters it 
        count = 0

        file = open(self.Html_source)
        data = file.read()
        soup = BeautifulSoup(data, 'lxml')
        all_divs = soup.find_all('li',class_='badgeList__item',)
        scrapper_links = [self.Base_url + a_href.div.div.a['href'] for a_href in all_divs]

        for link in scrapper_links:
            count += 1
            print("{} ------> {}".format(count,link))

        count = 0

        data = []
        for s_link in scrapper_links:
            user_page = requests.get(s_link, headers=self.headers)
            text = user_page.content
            inner_pagee = text.decode()
            all_emails = re.findall(r'[w\w.-]+@[\w\.-]+', inner_pagee)
            if all_emails:
                count += 1
                print("{} Scraping Emails: {}".format(count, all_emails[0]))
                data.append(all_emails[0])
                new_data = list(set(data))

        data1 =[]
        for x in new_data:
            x = re.sub('[.]$','',x)
            data1.append(x)
        print(data1)


        with open('test.csv', "w", encoding="utf-8") as output:
            writer = csv.writer(output, lineterminator='\n')
            for val in data1:
                writer.writerow([val])
但我一直得到以下错误:

UnicodeDecodeError:“charmap”编解码器无法解码位置中的字节0x81 229393:角色映射到


你知道如何解决这个问题吗?

你打开的文件不是utf-8格式,请检查格式(编码)并用它代替utf-8

试一试

  encoding='utf-8-sig'