此selenium项目的正确元素类型是什么?

2024-10-05 12:27:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我在想办法,但什么都没用。 我正在尝试使用网络刮板,在该网站上打印所有热门交易百分比: “https://shadowpay.com/en?price_from=0.00&price_to=34.00&game=csgo&hot_deal=true” 但是我遇到了一个错误(我已经尝试了很多方法来解决这个问题,但我认为这是因为我缺乏HTML) 错误是我想要打印的元素(“percent-hot-deal\uuu block”)不是类名,但我已经尝试了很多按选项查找元素的方法,但都没有效果,所以我来这里。 代码:

import pandas as pd
from bs4 import BeautifulSoup as bs
from selenium import webdriver
import requests
import time
#
perc = []
#
PATH = 'C:/Users/Matiss/Documents/chromedriver_win32/chromedriver.exe'
driver = webdriver.Chrome(PATH)
driver.get("https://shadowpay.com/en?price_from=0.00&price_to=34.00&game=csgo&hot_deal=true")
#
dealblock = driver.find_elements_by_tag_name("span")
for deal in dealblock:
    header = deal.find_element_by_class_name("percent-hot-deal__block")
    print(header.text)
#
time.sleep(15)

driver.quit()

请帮助,如果我需要编辑任何内容,请务必发表评论。 另外,我知道导入这么多东西是没有用的,我在同一个文件上遵循其他教程


Tags: tofromhttpsimportcomgametruedriver
2条回答

基本上,您需要等待页面加载并获取元素

你可以这样做

driver.get("https://shadowpay.com/en?price_from=0.00&price_to=34.00&game=csgo&hot_deal=true")
driver.implicitly_wait(5)
dealblock = driver.find_elements_by_css_selector("span.percent-hot-deal__block")
#print(len(dealblock))
for deal in dealblock:
    print(deal.text)

更理想的方法是:

wait = WebDriverWait(driver, 10)
driver.get("https://shadowpay.com/en?price_from=0.00&price_to=34.00&game=csgo&hot_deal=true")
dealblock = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "span.percent-hot-deal__block")))
#print(len(dealblock))
for deal in dealblock:
    print(deal.text)

进口

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC

试试这个:

import json
import requests 
r = requests.get('https://api.shadowpay.com/api/market/get_items?types=[]&exteriors=[]&rarities=[]&collections=[]&item_subcategories=[]&float={"from":0,"to":1}&price_from=0.00&price_to=34.00&game=csgo&hot_deal=true&stickers=[]&count_stickers=[]&short_name=&search=&stack=false&sort=desc&sort_column=price_rate&limit=50&offset=0')

for i in range(len(r.json()["items"])):
    try:
        print(r.json()["items"][i]["collection"]["name"], ",", r.json()["items"][i]["discount"])
    except Exception as err:
        print(err)

相关问题 更多 >

    热门问题