[英]How do I Copy the HTML code of a website to a text file in python?
I have tried this, but I get this error: 'UnicodeEncodeError: 'charmap' codec can't encode characters in position 286381-286385: character maps to ' import requests from bs4 import BeautifulSoup我试过这个,但我得到这个错误:'UnicodeEncodeError:'charmap'编解码器无法编码 position 286381-286385 中的字符:字符映射到'从 bs4 导入请求导入 BeautifulSoup
def main():
f = open("sites.text", 'w')
page = requests.get("https://stackoverflow.com")
soup = BeautifulSoup(page.content, "html.parser")
f.write(str(soup))
f.close()
if __name__ == '__main__':
main()
Try this:尝试这个:
with open("example", "w", encoding="utf8") as f:
page = requests.get("https://www.google.com/").text
soup = BeautifulSoup(page, "lxml")
f.write(str(soup))
You need to encode (also possible with soup.encode("utf-8")) and use text attribute of response object.您需要编码(也可以使用 soup.encode("utf-8"))并使用响应 object 的文本属性。
response.text returns the content of the response, in unicode. response.text 返回响应的内容,在 unicode 中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.