Heres บางอย่า async รหัสเพื่อให้เป็นคิวสำหรับอเมซอนหากับ selenium ทำไมฉันถึง AttributeError? ที่__init__ไม่ได้ทำงานอยู่?

0

คำถาม

นี่คือรหัสฉันใช้เพื่อการค้นหาอเมซอนออกจากทอร์นาโดเว็บแอพ ตอนนี้ฉันมีเพียงหนึ่งของพวกนี้ชั้นเรียน(และหนึ่ง webdriver)instantiated. ฉันอาจต้องการเป็นสระว่ายน้ำของพวกเขาอยู่ในอนาคต(ใช่ฉันรู้ว่ามันมี selenum เส้นกริดแต่นั่นไม่ใช่ของฉันคำถาม.) คำถามคือทำไมเป็น__init__เห็นได้ชัดว่าไม่ได้ทำงานอยู่? ทำไมถึงมีการ attributeError สำหรับเข้าถึงตัวเองคิว variance ซึ่งมันเห็นได้ชัดว่ากำหนดไว้ใน__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/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'
attributeerror class init python
2021-11-24 06:01:11
1

คำตอบที่ดีที่สุด

0

ยังไง rediciulous...ฉันบอกชื่อของ__init__ฟังก์ชัน__init___.

ผมซ่อมกันแมลง refactored และนี่คือห้องทำงานรหัส

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")))
2021-11-24 06:21:16

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่