简体   繁体   English

Python 网页抓取(请求,BeautifulSoup)

[英]Python web scrape (requests, BeautifulSoup)

I am trying to write a simple web scrape script so I wrote this code and I got an error.我正在尝试编写一个简单的网页抓取脚本,所以我编写了这段代码,但出现错误。

import requests
from bs4 import BeautifulSoup

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

soup = BeautifulSoup(r.content)

print(soup.prettify())

And I am getting an error saying:我收到一条错误消息:

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>

I am using Python 3.4.0我正在使用 Python 3.4.0

So can anyone tell what is going on?那么有谁能告诉我这是怎么回事吗?

I belive this is a Encode problem: try add a encode type on return string with:我相信这是一个编码问题:尝试在返回字符串上添加一个编码类型:

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

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

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