如何解决"Unresolved attribute reference for class"
How to solve "Unresolved attribute reference for class"
我一直在做一个小项目,它是一个网络爬虫模板。我在 pycharm 中遇到问题,收到警告 Unresolved attribute reference 'domain' for class 'Scraper'
from abc import abstractmethod
import requests
import tldextract
class Scraper:
scrapers = {}
def __init_subclass__(scraper_class):
Scraper.scrapers[scraper_class.domain] = scraper_class # Unresolved attribute reference 'domain' for class 'Scraper'
@classmethod
def for_url(cls, url):
k = tldextract.extract(url)
# Returns -> <scraper.SydsvenskanScraper object at 0x000001E94F135850> & Scraped BBC News<!DOCTYPE html><html Which type annotiation?
return cls.scrapers[k.registered_domain](url)
@abstractmethod
def scrape(self):
pass
class BBCScraper(Scraper):
domain = 'bbc.co.uk'
def __init__(self, url):
self.url = url
def scrape(self):
rep = requests.Response = requests.get(self.url)
return "Scraped BBC News" + rep.text[:20] # ALL HTML CONTENT
class SydsvenskanScraper(Scraper):
domain = 'sydsvenskan.se'
def __init__(self, url):
self.url = url
def scrape(self):
rep = requests.Response = requests.get(self.url)
return "Scraped Sydsvenskan News" + rep.text[:20] # ALL HTML CONTENT
if __name__ == "__main__":
URLS = ['https://www.sydsvenskan.se/', 'https://www.bbc.co.uk/']
for urls in URLS:
get_product = Scraper.for_url(urls)
r = get_product.scrape()
print(r)
当然我可以忽略它,因为它正在工作,但我不喜欢忽略警告,因为我相信 pycharm 很聪明,应该解决警告而不是忽略它,我想知道是什么原因它警告我吗?
只要告诉你的Scraper
class这个属性存在
class Scraper:
scrapers = {}
domain: str
def __init_subclass__(scraper_class):
Scraper.scrapers[scraper_class.domain] = scraper_class
关于如何删除此警告有几个不同的级别:
- 分配默认值:
class Scraper:
scrapers = {}
domain = None # Or a sensible value of one exists
- 您可以附加或交替注释类型。
from typing import ClassVar
class Scraper:
scrapers: ClassVar[dict[str, 'Scraper']] = {}
domain: ClassVar[str]
请注意,ClassVar
是必需的,否则将假定它们是实例属性。
要忽略它,输入
# noinspection PyUnresolvedReferences
在导致警告的行上方的行上。
我一直在做一个小项目,它是一个网络爬虫模板。我在 pycharm 中遇到问题,收到警告 Unresolved attribute reference 'domain' for class 'Scraper'
from abc import abstractmethod
import requests
import tldextract
class Scraper:
scrapers = {}
def __init_subclass__(scraper_class):
Scraper.scrapers[scraper_class.domain] = scraper_class # Unresolved attribute reference 'domain' for class 'Scraper'
@classmethod
def for_url(cls, url):
k = tldextract.extract(url)
# Returns -> <scraper.SydsvenskanScraper object at 0x000001E94F135850> & Scraped BBC News<!DOCTYPE html><html Which type annotiation?
return cls.scrapers[k.registered_domain](url)
@abstractmethod
def scrape(self):
pass
class BBCScraper(Scraper):
domain = 'bbc.co.uk'
def __init__(self, url):
self.url = url
def scrape(self):
rep = requests.Response = requests.get(self.url)
return "Scraped BBC News" + rep.text[:20] # ALL HTML CONTENT
class SydsvenskanScraper(Scraper):
domain = 'sydsvenskan.se'
def __init__(self, url):
self.url = url
def scrape(self):
rep = requests.Response = requests.get(self.url)
return "Scraped Sydsvenskan News" + rep.text[:20] # ALL HTML CONTENT
if __name__ == "__main__":
URLS = ['https://www.sydsvenskan.se/', 'https://www.bbc.co.uk/']
for urls in URLS:
get_product = Scraper.for_url(urls)
r = get_product.scrape()
print(r)
当然我可以忽略它,因为它正在工作,但我不喜欢忽略警告,因为我相信 pycharm 很聪明,应该解决警告而不是忽略它,我想知道是什么原因它警告我吗?
只要告诉你的Scraper
class这个属性存在
class Scraper:
scrapers = {}
domain: str
def __init_subclass__(scraper_class):
Scraper.scrapers[scraper_class.domain] = scraper_class
关于如何删除此警告有几个不同的级别:
- 分配默认值:
class Scraper:
scrapers = {}
domain = None # Or a sensible value of one exists
- 您可以附加或交替注释类型。
from typing import ClassVar
class Scraper:
scrapers: ClassVar[dict[str, 'Scraper']] = {}
domain: ClassVar[str]
请注意,ClassVar
是必需的,否则将假定它们是实例属性。
要忽略它,输入
# noinspection PyUnresolvedReferences
在导致警告的行上方的行上。