加入收藏 | 设为首页 | 会员中心 | 我要投稿 北几岛 (https://www.beijidao.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Python3获取新浪微博内容乱码问题

发布时间:2021-05-21 05:58:42 所属栏目:大数据 来源: https://www.jb51.cc
导读:用python获取新浪微博最近发布内容的时候调用 public_timeline()函数的返回值是个jsonDict对象,首先需要将该对象通过json.dumps函数转换成字符串,然后对该字符串用GBK进行编码和解码,注意编码的时候函数encode的第二个参数要使用ignore(默认是strict),以

用python获取新浪微博最近发布内容的时候调用 public_timeline()函数的返回值是个jsonDict对象,首先需要将该对象通过json.dumps函数转换成字符串,然后对该字符串用GBK进行编码和解码,注意编码的时候函数encode的第二个参数要使用ignore(默认是strict),以防止当解码错误的时候报错而使程序意外退出 。

import webbrowser
import sinaweibopy3  
from time import sleep
import json
def getLotsOfWeibo(searchStr):
    APP_KEY='你的key'
    APP_SECRET='你的密码'
    REDIRECT_URL='https://api.weibo.com/oauth2/default.html'
    client=sinaweibopy3.APIClient(app_key=APP_KEY,app_secret=APP_SECRET,redirect_uri=REDIRECT_URL)
    url = client.get_authorize_url()
    print(url)
    webbrowser.open_new(url)
    result = client.request_access_token(
            input("please input code : "))  # Enter the CODE obtained in the authorized address
    print(result)
    client.set_access_token(result.access_token,result.expires_in)
    t=client.public_timeline()
    t1=json.dumps(t,ensure_ascii=False)
    t1=t1.encode('gbk','ignore').decode('gbk')#采用GBK打印,忽略编码错误否则会报错
    print (t1)  # get the latest public Weibo
来源:我的个人博客

(编辑:北几岛)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读