weixin_44435050
ydtj
采纳率0%
2019-08-06 23:37

爬取知乎信息报错--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条回答

为你推荐