【问题标题】:using request and beautiful soup module in python在 python 中使用请求和漂亮的汤模块
【发布时间】:2020-08-26 03:07:16
【问题描述】:

我的代码很短,但它给出了意外的错误

import bs4

url = "https://www."+input("Enter The Name of the website: ")+".com"
req = re.get(url)
html_text = req.text
htmls = bs4.BeautifulSoup(html_text, "html.parser").prettify()
with open("facebook.html", "w+") as file:
    file.write(htmls)
Traceback (most recent call last):
  File "C:\Users\Tanay Mishra\PycharmProjects\First_Project\Web Scraping\web.py", line 9, in <module>
    file.write(htmls)
  File "C:\Users\Tanay Mishra\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 character '\u200b' in position 756: character maps to <undefined>```

【问题讨论】:

  • 请发布异常消息。

标签: python beautifulsoup python-requests


【解决方案1】:

\u200b 表示 Unicode 中的 ZERO WIDTH SPACE。尝试在 open() 函数中指定encoding="utf-8"。另一个好的做法是使用Response 对象的.content 属性并让BeautifulSoup 猜测编码:

import bs4
import requests


url = "https://www."+input("Enter The Name of the website: ")+".com"
req = requests.get(url)
htmls = bs4.BeautifulSoup(req.content, "html.parser").prettify()
with open("facebook.html", "w+", encoding="utf-8") as file:
    file.write(htmls)

【讨论】:

  • 感谢 Andrej Kesely 的解决方案。
猜你喜欢
  • 2018-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-28
  • 2021-06-22
  • 2013-03-21
  • 2018-11-03
  • 1970-01-01
相关资源
最近更新 更多