当前位置: 首页 > 面试题库 >

UnicodeEncodeError:'cp949'编解码器无法在位置90中编码字符'\ u20a9':非法的多字节序列

韶硕
2023-03-14
问题内容

我是python初学者。

我正在尝试抓取Google Play商店并导出到csv文件。但是我收到一条错误消息。

UnicodeEncodeError: 'cp949' codec can't encode character '\u20a9' in position 90: illegal multibyte sequence

这是我的源代码。

当我命令打印时,它可以工作。但是在导出到csv文件时显示错误消息

请帮我

from bs4 import BeautifulSoup
import urllib.request
import urllib.parse
import codecs
import json
import pickle
from datetime import datetime
import sys
import csv
import os


req = 'https://play.google.com/store/search?q=hana&c=apps&num=300'



response = urllib.request.urlopen(req)
the_page = response.read()
soup = BeautifulSoup(the_page)


#app_link  = soup.find('a', {'class' : 'title'})
#app_url = app_link.get('href')





for div in soup.findAll( 'div', {'class' : 'details'} ):
    title = div.find( 'a', {'class':'title'} )
    #print(title.get('href')) 
    app_url = title.get('href')

    app_details={}


    g_app_url = 'https://play.google.com' + app_url

    app_response = urllib.request.urlopen(g_app_url)
    app_page = app_response.read()
    soup = BeautifulSoup(app_page)
    #print(soup)


    #print( g_app_url )
    title_div = soup.find( 'div', {'class':'document-title'} )
    app_details['title'] = title_div.find( 'div' ).get_text().strip()

    subtitle = soup.find( 'a', {'class' : 'document-subtitle primary'} )
    app_details['developer'] = subtitle.get_text().strip()
    app_details['developer_link'] = subtitle.get( 'href' ).strip()

    price_buy_span = soup.find( 'span', {'class' : 'price buy'} )
    price = price_buy_span.find_all( 'span' )[-1].get_text().strip()
    price = price[:-4].strip() if price != 'Install' else 'Free' 
    app_details['price'] = price

    rating_value_meta = soup.find( 'meta', {'itemprop' : 'ratingValue'} )
    app_details['rating'] = rating_value_meta.get( 'content' ).strip()

    reviewers_count_meta = soup.find( 'meta', {'itemprop' : 'ratingCount'} )
    app_details['reviewers'] = reviewers_count_meta.get( 'content' ).strip()

    num_downloads_div = soup.find( 'div', {'itemprop' : 'numDownloads'} )
    if num_downloads_div: app_details['downloads'] = num_downloads_div.get_text().strip()

    date_published_div = soup.find( 'div', {'itemprop' : 'datePublished'} )
    app_details['date_published'] = date_published_div.get_text().strip()

    operating_systems_div = soup.find( 'div', {'itemprop' : 'operatingSystems'} )
    app_details['operating_system'] = operating_systems_div.get_text().strip()

    content_rating_div = soup.find( 'div', {'itemprop' : 'contentRating'} )
    app_details['content_rating'] = content_rating_div.get_text().strip()

    category_span = soup.find( 'span', {'itemprop' : 'genre'} )
    app_details['category'] = category_span.get_text()
    #print(app_details)


    with open('result.csv', 'w') as f:  # Just use 'w' mode in 3.x
        w = csv.DictWriter(f, app_details.keys())
        w.writeheader()
        w.writerow(app_details)

问题答案:

Python 3以语言环境默认编码打开文本文件;如果该编码无法处理您尝试写入的Unicode值,请选择其他编解码器:

with open('result.csv', 'w', encoding='UTF-8', newline='') as f:

它将所有Unicode字符串编码为UTF-8,该编码可以处理所有Unicode标准。

请注意,该csv模块建议您使用newline=''来打开文件,以防止换行。

您还需要在循环外仅 一次 打开文件for

with open('result.csv', 'w') as f:  # Just use 'w' mode in 3.x
    fields = ('title', 'developer', 'developer_link', 'price', 'rating', 'reviewers',
              'downloads', 'date_published', 'operating_system', 'content_rating',
              'category')
    w = csv.DictWriter(f, )
    w.writeheader()

    for div in soup.findAll( 'div', {'class' : 'details'} ):
        #
        # build app_details
        #

        w.writerow(app_details)


 类似资料:
  • 问题内容: 我想从网址获取html内容,并使用正则表达式解析html内容。但是html内容具有一些多字节字符。所以我遇到了标题中描述的错误。 有人可以告诉我如何解决这个问题吗? 问题答案: 您需要编辑问题以显示(1)您使用的代码(2)完整的错误和回溯(3)涉及的网址(4)您尝试将其 编码 为gbk的 unicode 字符是什么 您似乎已经从html内容中的原始字节中获得了unicode字符-怎么样

  • 问题内容: 当上传具有非ASCII字符的文件时,出现UnicodeEncodeError: 我使用MySQL,nginx和FastCGI运行Django 1.2。 根据Django Trac数据库,这是已解决的问题,但是我仍然有问题。欢迎提供有关如何修复的任何建议。 编辑:这是我的图像字段: 问题答案: 在对此进行更多调查之后,我发现我尚未在我的主要Nginx配置文件中设置字符集: 通过添加以上内

  • 问题内容: 我正在尝试抓取一个网站,但这给我一个错误。 我正在使用以下代码: 我收到以下错误: 我该怎么做才能解决此问题? 问题答案: 我通过添加将其修复。 那意味着变成。

  • 问题内容: 当我尝试在数据库中插入外来字符时,可能是什么导致此错误? 而我该如何解决呢? 谢谢! 问题答案: 字符U + 201C左双引号在Latin-1(ISO-8859-1)编码中不存在。 这 是 目前在代码页1252(西欧)。这是Windows特定的编码,基于ISO-8859-1,但会将多余的字符放入0x80-0x9F范围内。代码页1252通常与ISO-8859-1混淆,这是一种令人烦恼但现

  • 问题内容: 我正在尝试制作一个从sqlite3数据库中获取数据的脚本,但是遇到了问题。 数据库中的字段为text类型,并且包含html格式的文本。见下面的文字 尝试提取数据的python代码如下。 是否有人对如何将其打印/写入文件有任何想法。是的,我知道这是打印到stdout的,但是当我尝试写入文件时会出现相同的UnicodeEncodeError。我尝试了文件对象的写入方法和。 问题答案: 当您

  • 我正试图抓取一个网站,但它给了我一个错误。 我正在使用以下代码: 我得到了以下错误: 我能做些什么来解决这个问题?