Python TypeError:CLA中的字符串索引必须是整数

2024-10-03 11:25:39 发布

您现在位置:Python中文网/ 问答频道 /正文

当我在没有面向对象编程的情况下使用这段代码时,它是有效的。你知道吗

但是当我想在类print(link['href])中使用它时,会出现以下错误:

TypeError: string indices must be integers

有什么问题?你知道吗

class Link(object):        
  def get_linkblock(self,url):
    import httplib2
    from bs4 import BeautifulSoup as bs
    from bs4 import SoupStrainer        
    self.url = url
    print(self.url)        
    self.http = httplib2.Http()
    self.status, self.response = self.http.request(self.url)
    for link in bs(self.response, "lxml", parse_only=SoupStrainer('a')):
      print(link['href']) # <---- PROBLEM IS HERE

Tags: 代码fromimportselfhttpurlbsresponse