【问题标题】:The code doesn't run while I run it on my computer but it works on the tutorial当我在我的计算机上运行代码时,它没有运行,但它适用于教程
【发布时间】:2020-05-19 07:29:15
【问题描述】:
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]

【问题讨论】:

  • 什么是“不运行”?你的代码中的缩进和这里的一样吗?
  • 错误信息是什么?
  • 对我来说运行良好 ...
  • 可以添加错误信息吗?
  • @JoséRodrigues 它是如何运行的?它在我的笔记本电脑上不起作用

标签: python python-3.x web-scraping beautifulsoup python-requests


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多