小编Ory*_*yon的帖子

Python 中的抓取错误:“charmap”编解码器无法编码字符/无法将 str 连接到字节

当我尝试从“url”中抓取一些带有 Finish-Names 的文本时,出现上述错误。我尝试过的解决方案和相应的错误,在代码中注释如下。我既不知道如何解决这些问题,也不知道确切的问题是什么。我是 Python 初学者。任何帮助表示赞赏。

我的代码:

from lxml import html
import requests

page = requests.get('url')

site = page.text  # ERROR -> 'charmap' codec can't encode character '\x84' in  
      #  position {x}: character maps to <undefined>
# site = site.encode('utf-8', errors='replace')  # ERROR -> can't concat str to bytes
# site = site.encode('ascii', errors='replace')  # ERROR -> can't concat str to bytes

with open('url.txt', 'a') as file:
    try:
        file.write(site + '\n')
    except Exception as err:
        file.write('an ERROR occured: ' + str(err) …
Run Code Online (Sandbox Code Playgroud)

python web-scraping python-requests

2
推荐指数
1
解决办法
1万
查看次数

标签 统计

python ×1

python-requests ×1

web-scraping ×1