我已经使用python在类中创建了一个scipt来从网页中收集问题链接。我在脚本中使用了❯ k get nodes -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
aks-nodepool1-32797235-0 Ready agent 4h v1.11.4 10.240.0.4 <none> Ubuntu 16.04.5 LTS 4.15.0-1030-azure docker://3.0.1
aks-nodepool1-32797235-3 Ready agent 4h v1.11.4 10.240.0.6 <none> Ubuntu 16.04.5 LTS 4.15.0-1030-azure docker://3.0.1
❯ ks get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE
cluster-autoscaler-84984799fd-22j42 1/1 Running 0 2h 10.244.1.5 aks-nodepool1-32797235-3 <none>
heapster-5d6f9b846c-g7qb8 2/2 Running 0 1h 10.244.0.16 aks-nodepool1-32797235-0 <none>
kube-dns-v20-598f8b78ff-8pshc 4/4 Running 0 3h 10.244.1.4 aks-nodepool1-32797235-3 <none>
kube-dns-v20-598f8b78ff-plfv8 4/4 Running 0 1h 10.244.0.15 aks-nodepool1-32797235-0 <none>
kube-proxy-fjvjv 1/1 Running 0 1h 10.240.0.6 aks-nodepool1-32797235-3 <none>
kube-proxy-szr8z 1/1 Running 0 1h 10.240.0.4 aks-nodepool1-32797235-0 <none>
kube-svc-redirect-2rhvg 2/2 Running 0 4h 10.240.0.4 aks-nodepool1-32797235-0 <none>
kube-svc-redirect-r2m4r 2/2 Running 0 4h 10.240.0.6 aks-nodepool1-32797235-3 <none>
kubernetes-dashboard-68f468887f-c8p78 1/1 Running 0 4h 10.244.0.7 aks-nodepool1-32797235-0 <none>
metrics-server-5cbc77f79f-44f9w 1/1 Running 0 4h 10.244.0.3 aks-nodepool1-32797235-0 <none>
tiller-deploy-57f988f854-z9qln 1/1 Running 0 4h 10.244.0.8 aks-nodepool1-32797235-0 <none>
tunnelfront-7cf9d447f9-56g7k 1/1 Running 0 4h 10.244.0.2 aks-nodepool1-32797235-0 <none>
方法来打印结果。但是,当我尝试打印它时,输出为None。
这里的主要目的是使__str__()
此方法在我的以下脚本中起作用。
我要去哪里了,该如何解决?
__str__()
答案 0 :(得分:1)
您没有打印正确的东西。请检查文档,了解如何使用此类方法。
crawler = DataSourcer(URL)
crawler.fetch()
print(crawler)
答案 1 :(得分:1)
您的方法很好,看来您打印不正确。您正在打印crawler.fetch()
,它返回None
。
import requests
from urllib.parse import urljoin
from bs4 import BeautifulSoup
URL = "https://stackoverflow.com/questions/tagged/web-scraping"
class DataSourcer:
def __init__(self,link):
self.link = link
self.datalist = []
def fetch(self):
res = requests.get(self.link)
soup = BeautifulSoup(res.text,"lxml")
for ilink in soup.select(".summary .question-hyperlink"):
self.datalist.append(urljoin(self.link,ilink.get("href")))
def __str__(self):
return self.datalist
if __name__ == '__main__':
crawler = DataSourcer(URL)
crawler.fetch()
print(crawler.__str__())
答案 2 :(得分:0)
print(crawler.fetch())将打印fetch()方法的返回值(没有返回值) 您应该做的是:
crawler.fetch()
print(crawler)