我试图找到一些东西,但没有工作。我正试图使用一个网页刮板,打印所有的热门交易百分比在这个网站上:' https://shadowpay.com/en?price_from=0.00&price_to=34.00&game=csgo&hot_deal=true '但是我遇到了一个错误(我已经尝试了很多方法来解决这个问题,但我认为这是我缺乏HTML)错误是我想要打印的元素('percent-hot-deal__block')不是一个类名,但我已经尝试了很多find_element_by选项,没有工作,所以我来这里。代码:
import pandas as pd
from bs4 import BeautifulSoup as bs
from selenium import webdriver
import requests
import time
#
perc = []
#
PATH = 'C:/Users/<user__name>/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()
请帮忙,如果我需要编辑什么,一定要评论。此外,我知道导入这么多东西是没有用的,我在同一个文件上遵循其他教程。
基本上,您需要等待页面加载并获取元素。
你可以这样做。
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)