亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

如何在 Python 中使用 ul-li 下拉列表抓取網站?

如何在 Python 中使用 ul-li 下拉列表抓取網站?

森林海 2023-03-08 16:02:55
基于問題Scraping a specific website with a search box and javascripts in Python,我試圖從網站https://www.msci.com/esg-ratings/中獲取公司評級 ,主要是在搜索框,在下拉菜單中選擇該名稱的所有選項(“RIO TINTO LIMITED”和“RIO TINTO PLC”此處為“rio tinto”),并在兩者的右上角獲得帶有評級的圖片。但是,我在處理建議公司的 ul-li 退出菜單時遇到了麻煩:from selenium import webdriverfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.common.by import Byoptions = webdriver.ChromeOptions()options.add_argument('-headless')options.add_argument('-no-sandbox')options.add_argument('-disable-dev-shm-usage')options.add_argument('window-size=1920,1080')wd = webdriver.Chrome(options=options)wd.get('https://www.msci.com/esg-ratings')WebDriverWait(wd, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="_esgratingsprofile_keywords"]'))).send_keys("RIO TINTO")WebDriverWait(wd, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="ui-id-1"]/li[1]'))).click()#WebDriverWait(wd,10).until(EC.visibility_of_element_located((By.CSS_SELECTOR,"#_esgratingsprofile_esg-ratings-profile-header > div.esg-ratings-profile-header-ratingdata > div.ratingdata-container > div.ratingdata-outercircle.esgratings-profile-header-yellow > div")))print(wd.find_element_by_xpath('//*[@id="_esgratingsprofile_esg-ratings-profile-header"]/div[2]/div[1]/div[2]/div'))(代碼給出了 ElementClickInterceptedException。)如何訪問“RIO TINTO LIMITED”和“RIO TINTO PLC”所需的數據?
查看完整描述

1 回答

?
慕工程0101907

TA貢獻1887條經驗 獲得超5個贊

我在處理推薦公司的 ul-li 退出菜單時遇到了麻煩


這是預期的,因為element您的目標是通過dynamic腳本呈現的。options.add_argument('-headless')為了克服這一點,你將不得不避免。


你這里也有問題


print(wd.find_element_by_xpath('//*[@id="_esgratingsprofile_esg-ratings-profile-header"]/div[2]/div[1]/div[2]/div'))

您嘗試打印元素的位置。由于目標元素是icon由呈現的CSS,因此您不能使用print()來輸出它。相反,您需要將其保存為一個.png文件


with open('filename.png', 'wb') as file:

    file.write(driver.find_element_by_xpath('//*[@id="_esgratingsprofile_esg-ratings-profile-header"]/div[2]/div[1]/div[2]/div').screenshot_as_png)

然后根據您的需要使用它。


查看完整回答
反對 回復 2023-03-08
  • 1 回答
  • 0 關注
  • 100 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號