隨著NFTs越來越流行,追蹤其稀有性也是其中的一部分。 我試圖從這個網頁中檢索特質計數,當我檢查頁面的 "特質計數"(顯示為5)時,我看到如下內容:
<div class="flex-grow overflow-hidden">5< /div>
所以我使用以下代碼:
import requests #fetches html page content
from bs4 import BeautifulSoup #parses html page content[/span
#確保我們得到英文翻譯的標題。
headers = {"Accept-Language": "en-US, en;q=0.5"}。
#Get the contents of the page we're looking at by request the URL, timeout set to 5 mins.
results = requests.get('https://rarity.tools/boredapeyachtclub/view/1', headers=headers, timeout = 720)
#parse html content[/span]。
soup = BeautifulSoup(results.text, "html.parser")
#抓取容納公司資訊的容器。
apes_div = soup.find('div'/span>, class_='flex-grow overflow-hidden'/span>)
print(apes_div)
而且我得到的是None而不是5...
編輯
我試著用下面的代碼做了selenium,似乎得到了同樣的結果:
我試著用下面的代碼做了selenium,似乎得到了同樣的結果。
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox(executable_path="/usr/local/bin/geckodriver" )
driver.get('https://rarity.tools/boredapeyachtclub/view/1')
trait = driver.find_elements_by_xpath('//td[@class="flex-grow overflow-hidden"] ' )
print(trait)
uj5u.com熱心網友回復:
你會注意到,如果你在driver.find_elements_by_xpath一行設定一個斷點,它在頁面完成加載之前就已經執行了。
這就是你想要做的事情:
這就是你想要做的事情。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox(executable_path="bin/geckodriver.exe")
try:
driver.get('https://rarity.tools/boredapeyachtclub/view/1')
WebDriverWait(driver, 30).until(
EC.presence_of_element_located((By.CLASS_NAME, "p-0"/span>)
)
traits = driver.find_elements_by_xpath('//div[@class="flex-grow overflow-hidden"] ' )
print(traits[2] .text)
finally:
driver.quit()
我只是注意到一個帶有p-0類的元素被加載為所需的有效載荷的一部分,你也許能找到更干凈的東西。WebDriverWait部分使瀏覽器等待,直到滿足某些條件,在這種情況下,一個具有給定類的元素出現。
另外,你正在尋找一個td,但是你所期望的資料實際上是在一個div中 - 如果你試圖找到一個div,你會找到8個,而第三個恰好是你要找的那個。
try ... finally只是為了在你完成后清理瀏覽器。
(注意:我在 Windows 上試過,所以一定要把你自己的代碼替換回來,以指向正確的驅動程式)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/331287.html
標籤:
