【问题标题】:Python web scrape (requests, BeautifulSoup)Python 网页抓取(请求,BeautifulSoup)
【发布时间】:2016-02-21 22:27:18
【问题描述】:

我正在尝试编写一个简单的网络抓取脚本,所以我编写了这段代码,但出现了错误。

import requests
from bs4 import BeautifulSoup

r = requests.get('http://the website that I need.com')

soup = BeautifulSoup(r.content)

print(soup.prettify())

我收到一条错误消息:

Traceback (most recent call last):
  File "course.py", line 18, in <module>
    print(soup.prettify())
  File "C:\Python34\lib\encodings\cp437.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u203a' in position
32558: character maps to <undefined>

我正在使用 Python 3.4.0

那么谁能告诉我发生了什么?

【问题讨论】:

  • 尝试使用r.text 代替r.content
  • 我试过但没用

标签: python beautifulsoup python-requests


【解决方案1】:

我相信这是一个编码问题:尝试在返回字符串上添加编码类型:

编码为 UTF-8 的示例 汤 = BeautifulSoup(r.content.encode('uft-8'))

【讨论】:

  • 我试过但它不起作用它说:回溯(最近一次调用最后一次):文件“course.py”,第 10 行,在 汤 = BeautifulSoup(r.content.encode( 'uft-8')) AttributeError: 'bytes' 对象没有属性 'encode'
  • 我看到stackoverflow.com/questions/7219361/… prettify 方法编码字符集可以用 args 设置,例如:soup.prettify('utf-8')
猜你喜欢
  • 2020-04-20
  • 2020-07-24
  • 1970-01-01
  • 2022-01-18
  • 2018-04-25
  • 2014-06-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多