ydtj 2019-08-06 23:37 采纳率: 0%
浏览 1969

爬取知乎信息报错--TypeError: string indices must be integers


import requests
import json
import pymysql

num=0 #设定爬取次数
user_all=[] #存放本次运行的用户

def get_url(url): #获取链接内容
header_info = {
"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36',
}
user_url =url
response =requests.get(user_url, headers=header_info)
data = response.content
data = data.decode('utf-8') #设置字符集
return data

def get_follower(userID): #解析内容,获取关注用户
list=[]
url = 'https://www.zhihu.com/api/v4/members/'+userID+'/followees?' \
'include=data%5B*%5D.answer_count%2Carticles_count%2Cgender%2Cfollower_count%' \
'2Cis_followed%2Cis_following%2Cbadge%5B%3F(type%3Dbest_answerer)%5D.topics&offset=0&limit=20'
data = get_url(url)
data = json.loads(data)
print(data)
for user in data:
list.append(user['url_token'])
return list

get_follower('kaiqu')

报错信息
TypeError                                 Traceback (most recent call last)
<ipython-input-22-0194a6f553cc> in <module>
     32 
     33 
---> 34 get_follower('kaiqu')
     35 
     36 

<ipython-input-22-0194a6f553cc> in get_follower(userID)
     28     print(data)
     29     for user in data:
---> 30         list.append(user['url_token'])
     31     return list
     32 

TypeError: string indices must be integers
  • 写回答

1条回答 默认 最新

  • 毕小宝 博客专家认证 2019-08-07 08:58
    关注

    可能是 30 行的访问方法错误了。
    https://blog.csdn.net/weixin_43256057/article/details/83867876

    评论

报告相同问题?

悬赏问题

  • ¥15 用三极管设计—个共射极放大电路
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示