鱼C论坛

 找回密码
 立即注册
查看: 269|回复: 13

[已解决]python爬虫

[复制链接]
发表于 2024-4-1 15:46:02 | 显示全部楼层 |阅读模式
5鱼币
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
              <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>閿欒?鎻愮ず</title>
    <script type="text/javascript">
            function ref() {
                    var url = window.location.href;
                    if (url.indexOf("?") == -1) {
                            url += "?cacheModel=true";
                    } else {
                            url += "&cacheModel=true";
                    }
                    window.location.href = url;
            }
    </script>
  </head>
  <body>
          <center><h1>鏈嶅姟鍣ㄧ箒蹇?璇风◢鍊欏啀璇?.....</h1></center>
          <center><button onclick="ref();">鍒锋柊椤甸潰</button></center>
          <div style="display:none;"></div>
  </body>
</html>

这个是我爬取后的内容

response = requests.post(url, headers=he, data=json.dumps(data))
response.encoding = 'utf-8'
decoded_text1 = response.text
print(decoded_text1.encode('gbk', errors='replace').decode('gbk'))
鏈嶅姟鍣ㄧ箒蹇?璇风◢鍊欏啀璇?这个我应该怎么写才是显示中文呢
最佳答案
2024-4-1 15:46:03
tengke 发表于 2024-04-01 17:11
Traceback (most recent call last):
  File "D:\软 ...


非常抱歉,仍然引起了编码问题。看起来网页内容中包含了一些无法被GBK编码的特殊字符。在这种情况下,可以尝试使用`errors='ignore'`参数来忽略无法编码的字符,并打印出可显示的文本内容。请将代码修改如下所示:response = requests.post(url, headers=he, data=json.dumps(data))response.encoding = 'utf-8'try:    print(response.text)except UnicodeEncodeError as e:    decoded_text = response.content.decode('ISO-8859-1', errors='ignore')    print(decoded_text)使用`errors='ignore'`参数可以忽略掉无法编码的字符,但可能会导致一些信息丢失。如果您对完整的内容感兴趣,并且愿意接受一些乱码,请将代码修改为如下所示:response = requests.post(url, headers=he, data=json.dumps(data))response.encoding = 'utf-8'try:    print(response.text)except UnicodeEncodeError as e:    decoded_text = response.content.decode('ISO-8859-1', errors='replace')    print(decoded_text)这样应该能够处理大部分的编码问题。如果问题仍然存在,请提供更多关于网页内容和编码的信息,以便我更好地帮助您。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。

最佳答案

查看完整内容

非常抱歉,仍然引起了编码问题。看起来网页内容中包含了一些无法被GBK编码的特殊字符。在这种情况下,可以尝试使用`errors='ignore'`参数来忽略无法编码的字符,并打印出可显示的文本内容。请将代码修改如下所示:response = requests.post(url, headers=he, data=json.dumps(data))response.encoding = 'utf-8'try: print(response.text)except UnicodeEncodeError as e: decoded_text = response.content.decode('ISO- ...
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-4-1 15:46:03 | 显示全部楼层    本楼为最佳答案   
tengke 发表于 2024-04-01 17:11
Traceback (most recent call last):
  File "D:\软 ...


非常抱歉,仍然引起了编码问题。看起来网页内容中包含了一些无法被GBK编码的特殊字符。在这种情况下,可以尝试使用`errors='ignore'`参数来忽略无法编码的字符,并打印出可显示的文本内容。请将代码修改如下所示:response = requests.post(url, headers=he, data=json.dumps(data))response.encoding = 'utf-8'try:    print(response.text)except UnicodeEncodeError as e:    decoded_text = response.content.decode('ISO-8859-1', errors='ignore')    print(decoded_text)使用`errors='ignore'`参数可以忽略掉无法编码的字符,但可能会导致一些信息丢失。如果您对完整的内容感兴趣,并且愿意接受一些乱码,请将代码修改为如下所示:response = requests.post(url, headers=he, data=json.dumps(data))response.encoding = 'utf-8'try:    print(response.text)except UnicodeEncodeError as e:    decoded_text = response.content.decode('ISO-8859-1', errors='replace')    print(decoded_text)这样应该能够处理大部分的编码问题。如果问题仍然存在,请提供更多关于网页内容和编码的信息,以便我更好地帮助您。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-4-1 15:46:18 | 显示全部楼层
在爬取网页内容后,如果要正确显示中文,可以使用以下方法:

1. 指定response的编码为utf-8:
  
  1. response.encoding = 'utf-8'
复制代码


2. 通过`response.text`获取网页内容后,直接打印即可,不需要再进行编码转换:
  
  1. print(response.text)
复制代码


这样就能够正确显示中文了。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2024-4-1 16:09:04 | 显示全部楼层
response = requests.post(url, headers=he, data=json.dumps(data))
response.encoding = 'utf-8'
print(response.text) 这样写提示报错
Traceback (most recent call last):
  File "D:\软件\djangoProject\app\zspc.py", line 46, in <module>
    print(response.text)
UnicodeEncodeError: 'gbk' codec can't encode character '\ue1e4' in position 178: illegal multibyte sequence
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-4-1 16:09:29 | 显示全部楼层
tengke 发表于 2024-04-01 16:09
response = requests.post(url, headers=he, data=jso ...


这个错误提示表明在尝试使用GBK编码输出时,遇到了无法编码的字符。解决方法是使用Unicode编码输出,而不是GBK。可以将代码修改如下:
  1. response = requests.post(url, headers=he, data=json.dumps(data))
  2. response.encoding = 'utf-8'
  3. decoded_text = response.text.encode('latin1').decode('unicode_escape')
  4. print(decoded_text)
复制代码

这样就能正确显示中文了。如果还有其他编码问题,可以尝试使用不同的编码进行解码,找到适合的编码方式。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2024-4-1 16:12:45 | 显示全部楼层
Traceback (most recent call last):
  File "D:\软件\djangoProject\app\zspc.py", line 48, in <module>
    decoded_text = response.text.encode('latin1').decode('unicode_escape')
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 172-177: ordinal not in range(256)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-4-1 16:12:56 | 显示全部楼层
tengke 发表于 2024-04-01 16:12
Traceback (most recent call last):
  File "D:\软 ...


这个错误提示表明在尝试使用Latin-1编码进行解码时,遇到了无法编码的字符。我们可以使用更通用的编码方式,比如UTF-8。请将代码修改如下:

  1. response = requests.post(url, headers=he, data=json.dumps(data))
  2. response.encoding = 'utf-8'
  3. decoded_text = response.text.encode('latin1').decode('utf-8')
  4. print(decoded_text)
复制代码


这样就能正确显示中文了。如果还有其他编码问题,可以尝试使用不同的编码进行解码,找到适合的编码方式。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2024-4-1 16:15:44 | 显示全部楼层
Traceback (most recent call last):
  File "D:\软件\djangoProject\app\zspc.py", line 42, in <module>
    decoded_text = response.text.encode('latin1').decode('utf-8')
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 172-177: ordinal not in range(256)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-4-1 16:16:06 | 显示全部楼层
tengke 发表于 2024-04-01 16:15
Traceback (most recent call last):
  File "D:\软 ...


看起来问题可能出在编码转换的步骤上。让我们尝试简化处理,直接使用UTF-8编码解码。请将代码修改如下:

  1. response = requests.post(url, headers=he, data=json.dumps(data))
  2. response.encoding = 'utf-8'
  3. decoded_text = response.text
  4. print(decoded_text)
复制代码


这样应该就能够正确显示中文了。如果还有其他问题,请告诉我。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2024-4-1 16:44:33 | 显示全部楼层
Traceback (most recent call last):
  File "D:\软件\djangoProject\app\zspc.py", line 43, in <module>
    print(decoded_text)
UnicodeEncodeError: 'gbk' codec can't encode character '\ue1e4' in position 178: illegal multibyte sequence
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-4-1 16:44:48 | 显示全部楼层
tengke 发表于 2024-04-01 16:44
Traceback (most recent call last):
  File "D:\软 ...


非常抱歉,我犯了一个错误。在Python中,默认的输出编码是GBK,如果输出中包含无法编码的字符,则会引发UnicodeEncodeError错误。为了解决这个问题,我们可以尝试使用try-except来捕获UnicodeEncodeError错误,并使用`errors='replace'`参数将无法编码的字符替换为问号。代码示例如下:response = requests.post(url, headers=he, data=json.dumps(data))response.encoding = 'utf-8'try:    print(response.text)except UnicodeEncodeError as e:    print(response.text.encode('gbk', errors='replace').decode('gbk'))这样应该能够正确地显示中文了。如果还有其他问题,请随时告诉我。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2024-4-1 17:10:39 | 显示全部楼层
的确对应的html格式显示了 但是 对应文本内容还是鏈嶅姟鍣ㄧ箒蹇?璇风◢鍊欏啀璇
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-4-1 17:10:51 | 显示全部楼层
tengke 发表于 2024-04-01 17:10
的确对应的html格式显示了 但是 对应文本内容还是鏈嶅 ...


抱歉给您带来了困扰。根据您的描述,这个问题可能是由于网页内容的编码问题引起的。有些网页使用了非标准的编码,导致无法正确地解码中文字符。

为了解决这个问题,我们可以尝试其他的编码方式进行解码,比如使用`ISO-8859-1`编码。请将代码修改如下所示:

  1. response = requests.post(url, headers=he, data=json.dumps(data))
  2. response.encoding = 'utf-8'

  3. try:
  4.     print(response.text)
  5. except UnicodeEncodeError as e:
  6.     decoded_text = response.content.decode('ISO-8859-1')
  7.     print(decoded_text)
复制代码


这样应该能够更好地解码中文字符。如果仍然无法解决问题,请尝试其他常用的编码方式,如`gbk`、`utf-16`等。

另外,如果您能提供更多关于网页内容和编码的信息,我可以帮助您更准确地解决问题。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2024-4-1 17:11:26 | 显示全部楼层
Traceback (most recent call last):
  File "D:\软件\djangoProject\app\zspc.py", line 48, in <module>
    print(response.text)
UnicodeEncodeError: 'gbk' codec can't encode character '\ue1e4' in position 178: illegal multibyte sequence

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\软件\djangoProject\app\zspc.py", line 51, in <module>
    print(decoded_text)
UnicodeEncodeError: 'gbk' codec can't encode character '\x96' in position 177: illegal multibyte sequence
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-24 20:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表