当我使用没有OOP的代码时,它可以工作。
但是当我想在课程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