【问题标题】:Whats is the correct element type for this selenium project?这个硒项目的正确元素类型是什么?
【发布时间】:2021-04-28 06:54:46
【问题描述】:

我试图找出一些东西,但没有任何效果。 我正在尝试使用网络抓取工具来打印此网站上的所有热卖百分比: '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/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()

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

【问题讨论】:

    标签: python html css selenium web-scraping


    【解决方案1】:

    基本上你想等待页面加载并抓取元素。

    你可以这样做。

    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
    

    【讨论】:

      【解决方案2】:

      试试这个:

      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)
      

      【讨论】:

      • 但问题是,你会输入什么来获取皮肤名称?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-21
      • 2013-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多