尝试使用 PyPartpicker API 进行搜索时,是否有人熟悉“AttributeError: 'NoneType' object has no attribute 'get_text'”错误?`
甚至示例代码:
from pypartpicker import Scraper
# creates the scraper object
pcpp = Scraper()
# returns a list of Part objects we can iterate through
parts = pcpp.part_search("i7")
# iterates through every part object
for part in parts:
# prints the name of the part
print(part.name)
# gets the first product and fetches its URL
first_product_url = parts[0].url
# gets the Product object for the item
product = pcpp.fetch_product(first_product_url)
# prints the product's specs using the specs attribute
print(product.specs)
返回此错误:
Traceback (most recent call last):
File "/workspaces/pcpartpickertest/index.py", line 7, in <module>
parts = pcpp.part_search("i7")
File "/home/codespace/.python/current/lib/python3.10/site-packages/pypartpicker/scraper.py", line 232, in part_search
soup = self.__make_soup(f"{search_link}&page={i + 1}")
File "/home/codespace/.python/current/lib/python3.10/site-packages/pypartpicker/scraper.py", line 95, in __make_soup
if "Verification" in soup.find(class_="pageTitle").get_text():
AttributeError: 'NoneType' object has no attribute 'get_text'
我正在努力寻找除了官方 Github 页面之外的任何文档,因此如果有人可以提供任何文档或想法,我将不胜感激!
我已经尝试了一切,包括一起删除搜索对象,请帮忙!
是的,这是人们报告的最常见错误之一。问题在于您未在问题中包含的代码行。然而,错误消息告诉我们它发生在 scraper.py,第 95 行,在
__make_soup
。
关键就在声明中:
if "Verification" in soup.find(class_="pageTitle").get_text():
对
soup.find
的调用应返回一些对象引用,对 get_text
的调用将返回一些数据。但如果 soup.find
不成功,它不会返回任何对象(实际上它返回 None
)。所以调用 get_text
是不可能的,因为没有对象可以调用它。这会导致错误消息:
AttributeError:“NoneType”对象没有属性“get_text”
因此您需要进行一些调试,以尝试找出对
soup.find
的初始调用失败的原因。