鱼C论坛

 找回密码
 立即注册
查看: 2698|回复: 1

下载百度贴吧出现问题

[复制链接]
发表于 2017-9-5 16:14:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import urllib.request
  2. import urllib.parse
  3. import chardet

  4. def response(fileurl,filename):
  5.     print('正在加载%s'% filename)
  6.     Requ=urllib.request.Request(fileurl)
  7.     Requ.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.3368.400 QQBrowser/9.6.11974.400')
  8.     res=urllib.request.urlopen(Requ).read()
  9.     resde=chardet.detect(res)['encoding']
  10.     if resde == 'GB2312':
  11.         resde='GBK'
  12.     name=res.decode(resde)
  13.     return name,resde

  14. def write_tie(html,resde,filename):
  15.     print('正在保存%s' % filename)
  16.     with open(filename,'w',encoding=resde)as f:
  17.         f.write(html)
  18.         print('-'*20)


  19. def tieba_page(url,start_page,end_page):
  20.     for i in range(start_page,end_page+1):
  21.         pn=(i-1)*50
  22.         filename='第%s页.html' % i
  23.         fileurl=url+('&pn=%d' % pn)
  24.         html,resde=response(fileurl,filename)
  25.         write_tie(html,resde,filename)
  26.    


  27. if __name__=='__main__':
  28.     kw=input('请输入贴吧名:')
  29.     start_page=int(input('请输入起始页:'))
  30.     end_page=int(input('请输入结束页:'))
  31.     urld='https://tieba.baidu.com/f?'
  32.     key=urllib.parse.urlencode({'kw':kw})
  33.     print(type(key))
  34.     url=urld+key
  35.     tieba_page(url,start_page,end_page)
复制代码


Traceback (most recent call last):
  File "C:\Users\Administrator\Desktop\tieba\789.py", line 41, in <module>
    tieba_page(url,start_page,end_page)
  File "C:\Users\Administrator\Desktop\tieba\789.py", line 28, in tieba_page
    html,resde=response(fileurl,filename)
  File "C:\Users\Administrator\Desktop\tieba\789.py", line 13, in response
    name=res.decode(resde)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35-32\lib\encodings\cp1254.py", line 15, in decode
    return codecs.charmap_decode(input,errors,decoding_table)
UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 266: character maps to <undefined>
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-9-5 16:15:57 | 显示全部楼层
用这个chardet模块会出现问题,要是在read后面直接用utf-8解码就没有问题
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-23 17:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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