我正在嘗試運行一個腳本來簡單地在網站中找到一些數字,但它似乎不想讓我超過某個點。在這個腳本中:
from requests_html import HTMLSession
import requests
url = "https://auction.chimpers.xyz/"
try:
s = HTMLSession()
r = s.get(url)
except requests.exceptions.RequestException as e:
print(e)
r.html.render(sleep=1)
title = r.html.find("title",first=True).text
print(title)
divs_found = r.html.find("div")
print(divs_found)
meta_desc = r.html.xpath('//*[@id="description-view"]/div',first=True)
print(meta_desc)
price = r.html.find(".m-complete-info div",first=True)
print(price)
結果是:
Chimpers Genesis 100
[<Element 'div' id='app'>, <Element 'div' data-v-1d311e85='' id='m-connection' class=('manifold',) >, <Element 'div' id='description-view'>, <Element 'div' class=('manifold', 'm-complete-view')>, <Element 'div' data-v-cf8dbfe2=' ' class=('manifold', 'loading-screen')>, <Element 'div' class=('manifold-logo',)>] <Element 'div' class=('manifold', 'm-complete- view')>
無
[3.9s 完成]
網站:https ://auction.chimpers.xyz/
我試圖找到的資訊就在這里
顯然,在串列中列印出來的元素之外還有更多的 html 元素,但是每次我嘗試訪問它們時,即使使用 r.html.xpath("//*[@id="description-view"]/div/ div[2]/div/div[2]/span/span 1 ") 它會回傳 None 即使它是我通過谷歌檢查獲得的復制 xpath
這是什么原因以及我將如何處理它?
uj5u.com熱心網友回復:
我實際上不知道它是否可能與requests_html,但它與selenium.
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
from selenium.webdriver.chrome.options import Options
url = "https://auction.chimpers.xyz/"
class_names = ["m-price-label", "m-price-data"]
driver_options = Options()
driver_options.add_argument("--headless")
driver = webdriver.Chrome(options=driver_options)
driver.get(url)
results = {}
try:
for class_name in class_names:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, class_name)))
# Getting inner text of the html tag
results[class_name] = element.get_attribute("textContent")
finally:
driver.quit()
print(results)
隨意使用 Chrome 以外的其他網路驅動程式
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/428657.html
標籤:javascript Python html css python-请求-html
上一篇:通過div對齊專案2/3?
