繁体   English   中英

当我在我的计算机上运行代码时,它不会运行,但它适用于教程

[英]The code doesn't run while I run it on my computer but it works on the tutorial

import requests
from bs4 import BeautifulSoup
result = requests.get("https://www.google.com/") 
# print(result.status_code)
# print (result.headers)
src = result.content
#print(src)
soup = BeautifulSoup(src, 'lxml')
 links = soup.find_all("a")
 print (links)
 print ("\n")

当我在我的计算机上运行代码时,它不会运行,但它适用于教程。 该代码没有从我尝试安装 lxml 的打印(链接)运行,但它没有运行。

这是错误消息:

python -u C:\Users\HP\Desktop\scrapping\p1.py
] Traceback (most recent call last): File "C:\Users\HP\Desktop\scrapping\p1.py",
line 13, in print (links) File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\encodings\cp1252.py",
line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_table)[0] UnicodeEncodeError: 'charmap' codec can't encode characters in position 72-77: character maps to [Finished in 1.271s]

您面临的问题是links包含 unicode 字符,这些字符无法转换为 Windows 在命令提示符/控制台中使用的默认代码页。

尝试以下操作来更改代码页和 python 编码 io 流的方式(假设您使用的是python-3.2或更高版本)。

> chcp 65001
> set PYTHONIOENCODING=utf-8
> run-your-program-here

那时他们可能会就如何处理这些字符达成一致

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM