第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 Python 使用 Selenium Ubuntu EC2 解決

如何使用 Python 使用 Selenium Ubuntu EC2 解決

心有法竹 2023-03-08 16:01:08
我正在嘗試提取此頁面中“Nb B”列的所有數(shù)據(jù): https: //www.coteur.com/cotes-foot.php當(dāng)我從 Ubuntu PC 運行我的代碼時,它運行得很好,但是當(dāng)我嘗試使用 EC2 Ubuntu 時,它并沒有給我很好的回報。這是我的服務(wù)器:ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-20200611 (ami-0a63f96e85105c6d3)這是 python 腳本:#!/usr/bin/python3# -*- coding: utf--8 --*-from selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsoptions = Options()options.headless = Trueoptions.add_argument("window-size=1400,800")options.add_argument("--no-sandbox")options.add_argument("--disable-gpu")driver = webdriver.Chrome(options=options)driver.get('https://www.coteur.com/cotes-foot.php')#Store url associated with the soccer gamesurl_links = []for i in driver.find_elements_by_xpath('//a[contains(@href, "match/cotes-")]'):    url_links.append(i.get_attribute('href'))print(len(url_links), '\n')nb_bookies = []for i in driver.find_elements_by_xpath('//tr[@id and @role="row" ]/td[last()]'):    nb_bookies.append(i.text)print(nb_bookies)這是輸出:Traceback (most recent call last):  File "./coteurchrome.py", line 25, in <module>    url_links.append(i.get_attribute('href'))  File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 141, in get_attribute    self, name)  File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 636, in execute_script    'args': converted_args})['value']  File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute    self.error_handler.check_response(response)  File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response    raise exception_class(message, screen, stacktrace)selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document  (Session info: headless chrome=83.0.4103.116)
查看完整描述

1 回答

?
慕桂英3389331

TA貢獻2036條經(jīng)驗 獲得超8個贊

要打印href屬性的值,您必須為引入WebDriverWaitvisibility_of_all_elements_located()并且您可以使用以下任一定位器策略


使用XPATH:


driver.get("https://www.coteur.com/cotes-foot.php")

print([my_elem.get_attribute("href") for my_elem in WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.XPATH, "//a[contains(@href, 'match/cotes-')]")))])

使用CSS_SELECTOR:


driver.get("https://www.coteur.com/cotes-foot.php")

print([my_elem.get_attribute("href") for my_elem in WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "a[href*='match/cotes-']")))])

控制臺輸出:


['https://www.coteur.com/match/cotes-millwall-blackburn-rid1109557.html', 'https://www.coteur.com/match/cotes-cardiff-derby-county-rid1109558.html', 'https://www.coteur.com/match/cotes-sheffield-w-huddersfield-rid1109559.html', 'https://www.coteur.com/match/cotes-luton-queens-park-rangers-rid1109563.html', 'https://www.coteur.com/match/cotes-chelsea-norwich-rid1109746.html', 'https://www.coteur.com/match/cotes-atalanta-brescia-rid1109694.html', 'https://www.coteur.com/match/cotes-gil-vicente-tondela-rid1109683.html', 'https://www.coteur.com/match/cotes-benfica-vitoria-guimaraes-rid1109899.html', 'https://www.coteur.com/match/cotes-orlando-city-new-york-city-fc-rid1109292.html', 'https://www.coteur.com/match/cotes-philadelphia-union-inter-miami-cf-rid1109809.html', 'https://www.coteur.com/match/cotes-ural-ekaterinburg-arsenal-tula-rid1109831.html', 'https://www.coteur.com/match/cotes-spartak-moscou-terek-akhmat-grozni-rid1109833.html', 'https://www.coteur.com/match/cotes-krylya-sovetov-fc-krasnodar-rid1109834.html', 'https://www.coteur.com/match/cotes-moreirense-pacos-ferreira-rid1109682.html', 'https://www.coteur.com/match/cotes-brentford-preston-rid1109561.html', 'https://www.coteur.com/match/cotes-bristol-c-stoke-rid1109568.html', 'https://www.coteur.com/match/cotes-fk-bodo-glimt-kristiansund-bk-rid1109735.html', 'https://www.coteur.com/match/cotes-molde-viking-rid1109736.html', 'https://www.coteur.com/match/cotes-sandefjord-aalesund-rid1109737.html', 'https://www.coteur.com/match/cotes-stromsgodset-mjondalen-rid1109738.html', 'https://www.coteur.com/match/cotes-valerenga-fk-haugesund-rid1109740.html', 'https://www.coteur.com/match/cotes-shakhtar-donetsk-kolos-kovalivka-rid1109948.html']

注意:您必須添加以下導(dǎo)入:


from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.common.by import By

from selenium.webdriver.support import expected_conditions as EC


查看完整回答
反對 回復(fù) 2023-03-08
  • 1 回答
  • 0 關(guān)注
  • 101 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號