我使用的是BeautifulSoup+urllib 环境:Python3.5.2
该网站使用UTF-8编码
百度、谷歌上的方法我都试过了,但是还都是乱码:X{Oã8ÿ€ý>t ÓeW®ÉªŽp ÁÑ£eÙNÜÄi
相关代码如下
# -*- coding: UTF-8 -*-
from bs4 import BeautifulSoup
import urllib.request
import urllib
import io
import sys
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='UTF-8')
def get_token(requrl,header):
req = urllib.request.Request(url=requrl,headers=header)
response = urllib.request.urlopen(req)
print(type(response))
print (response.getcode())
the_page = response.read()
print(len(the_page))
soup = BeautifulSoup(the_page,"html.parser",from_encoding="iso-8859-15")
print(soup.prettify())
user_token = soup.form.input.input.input.input["value"] #get the user_token
return user_token
user_token = get_token(requrl,header)
测试了很久,觉得问题主要就是出现在 the_page = response.read() 这里,read()返回一个bytes类,然后这个东西只能用iso-8859-15解码才不报错,但是试了很多办法,依旧输出的乱码,求解
这个回答很有参考意义,和我情况很像,但是按他的方法还是不对https://www.oschina.net/question/1012422_145865
哪位大表哥帮帮我~~~
网页源码标识了utf-8,应该按照utf-8解码吧
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。