继承人一些异步代码为亚马逊搜索提供硒队列,为啥我得到 AttributeError? __init__ 没有运行?
Posted
技术标签:
【中文标题】继承人一些异步代码为亚马逊搜索提供硒队列,为啥我得到 AttributeError? __init__ 没有运行?【英文标题】:Heres some async code to provide a queue for amazon searches with selenium, why am i getting AttributeError? The __init__ is not running?继承人一些异步代码为亚马逊搜索提供硒队列,为什么我得到 AttributeError? __init__ 没有运行? 【发布时间】:2022-01-02 13:33:25 【问题描述】:这是我用来从我的 tornado 网络应用程序中搜索亚马逊的代码。目前我只实例化了其中一个类(和一个 webdriver)。我将来可能想要一个池(是的,我知道有硒网格,但这不是我的问题。)我的问题是,为什么 __init__ 显然没有运行?为什么访问在__init__中明确定义的self.queue var会出现attributeError?
import asyncio
import uuid
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.expected_conditions import presence_of_element_located
from selenium.common.exceptions import NoSuchElementException
class Searcher:
def __init___(self):
self.driver = webdriver.Firefox(executable_path="/home/felix/project/geckodriver")
self.wait = WebDriverWait(self.driver, 10)
self.driver.get("https://amazon.com/")
self.queue = []
async def _search(self, query):
self.driver.find_element(By.ID, "twotabsearchtextbox").send_keys("pixel" + Keys.RETURN)
self.wait.until(presence_of_element_located((By.XPATH,
"//div[contains(@class, 'sg-col-inner')]/div[contains(@class, 'a-section')]"
"/div[contains(@class, 'a-section')]")))
el = self.driver.find_elements(By.XPATH,
"//div[contains(@class, 'a-section')]/div[h2][not(contains(.,'Sponsored'))]"
"[contains(@class, 'a-section')]/..")
out=[]
for e in range(len(el)):
try:
out+=["price": el[e].find_element(By.XPATH, ".//span[contains(@class, 'a-price-whole')]").text + "." +
el[e].find_element(By.XPATH, ".//span[contains(@class, 'a-price-fraction')]").text,
"title": el[e].find_element(By.XPATH, ".//h2").text]
except NoSuchElementException:
pass
return out
def search(self, query):
_id = uuid.uuid4()
self.queue.append(_id)
async def help(self):
while 1:
if _id == self.queue[0]:
return await self._search(query)
await asyncio.sleep(0.5)
res = help(self)
self.queue.pop()
return res
a = Searcher()
print(asyncio.run(a.search("test")))
这是错误:
Traceback (most recent call last):
File "/home/felix/project/search.py", line 49, in <module>
print(asyncio.run(a.search("test")))
File "/home/felix/project/search.py", line 38, in search
self.queue.append(_id)
AttributeError: 'Searcher' object has no attribute 'queue'
【问题讨论】:
无论如何,最好避免将执行 IO 的复杂代码放在__init__
中。
我不确定你指的是什么代码,或者你为什么不指?
【参考方案1】:
多么荒谬……我已将 __init__ 函数命名为 __init___。
我已经修复了重构的其他错误,这是工作代码
import asyncio
import uuid
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.expected_conditions import presence_of_element_located
from selenium.common.exceptions import NoSuchElementException
class Searcher:
def __init__(self):
self.driver = webdriver.Firefox(executable_path="/home/felix/amcoin/geckodriver")
self.wait = WebDriverWait(self.driver, 10)
self.driver.get("https://amazon.com/")
self.queue = []
async def _search(self, query):
self.driver.find_element(By.ID, "twotabsearchtextbox").send_keys(query + Keys.RETURN)
self.wait.until(presence_of_element_located((By.XPATH,
"//div[contains(@class, 'sg-col-inner')]/div[contains(@class, 'a-section')]"
"/div[contains(@class, 'a-section')]")))
el = self.driver.find_elements(By.XPATH,
"//div[contains(@class, 'a-section')]/div[h2][not(contains(.,'Sponsored'))]"
"[contains(@class, 'a-section')]/..")
out=[]
for e in range(len(el)):
try:
out+=["price": el[e].find_element(By.XPATH, ".//span[contains(@class, 'a-price-whole')]").text + "." +
el[e].find_element(By.XPATH, ".//span[contains(@class, 'a-price-fraction')]").text,
"title": el[e].find_element(By.XPATH, ".//h2").text]
except NoSuchElementException:
pass
return out
async def search(self, query):
_id = uuid.uuid4()
self.queue.append(_id)
while 1:
if _id == self.queue[0]:
res = await self._search(query)
self.queue.pop()
return res
await asyncio.sleep(0.5)
a = Searcher()
print(asyncio.run(a.search("test")))
【讨论】:
以上是关于继承人一些异步代码为亚马逊搜索提供硒队列,为啥我得到 AttributeError? __init__ 没有运行?的主要内容,如果未能解决你的问题,请参考以下文章