修改代码 def get_user_info(self, selector): """ 获取用户昵称、微博数、关注数、粉丝数 """ try: # print('------Get user nickname, microblog_num, following, fans------') self.get_nickname() # get user's name user_info = selector.xpath("//div[@class='tip2']/*/text()") self.microblog_num = int(user_info[0][3: -1]) print(u'微博数: ', self.microblog_num) self.following = int(user_info[1][3: -1]) print(u'关注数: ', self.following) self.fans = int(user_info[2][3: -1]) print(u'粉丝数: ', self.fans) print('*' * 100) except Exception as e: print('Error: ', e) traceback.print_exc()
时间: 2024-03-22 18:40:24 浏览: 203
这段代码的问题在于没有返回用户信息,修改后的代码如下:
```
def get_user_info(self, selector):
""" 获取用户昵称、微博数、关注数、粉丝数 """
try:
# print('------Get user nickname, microblog_num, following, fans------')
self.get_nickname() # get user's name
user_info = selector.xpath("//div[@class='tip2']/*/text()")
self.microblog_num = int(user_info[0][3: -1])
print(u'微博数: ', self.microblog_num)
self.following = int(user_info[1][3: -1])
print(u'关注数: ', self.following)
self.fans = int(user_info[2][3: -1])
print(u'粉丝数: ', self.fans)
print('*' * 100)
return {"nickname": self.username, "microblog_num": self.microblog_num, "following": self.following, "fans": self.fans}
except Exception as e:
print('Error: ', e)
traceback.print_exc()
```
现在这个函数会返回一个字典,包含用户昵称、微博数、关注数和粉丝数。
阅读全文