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

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

從網(wǎng)頁上的錨標(biāo)記訪問詳細(xì)信息

從網(wǎng)頁上的錨標(biāo)記訪問詳細(xì)信息

人到中年有點(diǎn)甜 2022-06-22 17:46:52
我正在抓取一個網(wǎng)頁,我已經(jīng)設(shè)法使用 selenium 將表中的數(shù)據(jù)提取到一個 csv 文件中。我正在努力的是從表格每一行上的錨標(biāo)簽中獲取信息。我嘗試單擊表格的所有錨標(biāo)記以從相應(yīng)的 URL 獲取信息,但在單擊第一個 URL 后它停止了。它給出了一個錯誤消息:過時的元素引用:元素未附加到頁面文檔。我不確定這是解決這個問題的正確方法。這是我迄今為止嘗試過的代碼。如果代碼格式不正確,我很抱歉,我是 python 和 stackoverflow 的新手。 import csv import requests import time 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 browser = webdriver.Chrome(executable_path=r"D:\jewel\chromedriver.exe") browser.get(('https://e-sourcingni.bravosolution.co.uk/web/login.shtml')) signInButton = browser.find_element_by_css_selector(".only") signInButton.click() time.sleep(5) table = browser.find_element_by_css_selector(".list-table") for a in browser.find_elements_by_css_selector(".detailLink"):  a.click()  time.sleep(2)  browser.execute_script("window.history.go(-1)")  time.sleep(2) with open('output.csv', "w") as f:   writer = csv.writer(f)   writer.writerow(["S.No","Status","Organization","Project Title","First Publishing Date","Work Category","Listing Deadline"])  for row in table.find_elements_by_css_selector('tr'):    writer.writerow([d.text for d in row.find_elements_by_css_selector('td')]) browser.close()我需要的是從具有類 detailLink 的標(biāo)簽的 href 中獲取數(shù)據(jù)。我無法找到適當(dāng)?shù)姆椒▉韴?zhí)行此操作。
查看完整描述

2 回答

?
猛跑小豬

TA貢獻(xiàn)1858條經(jīng)驗(yàn) 獲得超8個贊

我使用普通的 for 循環(huán)來迭代表而不是 for each 循環(huán)。試試這個,讓我知道它是怎么回事。


import csv

import time

from selenium import webdriver


browser = webdriver.Chrome('/usr/local/bin/chromedriver')  # Optional argument, if not specified will search path.

browser.implicitly_wait(5)


browser.execute_script("window.open('about:blank','tab1');")

browser.switch_to.window("tab1")

browser.get(('https://e-sourcingni.bravosolution.co.uk/web/login.shtml'))

signInButton = browser.find_element_by_css_selector(".only")

signInButton.click()

time.sleep(5)

table = browser.find_element_by_css_selector(".list-table")

links=browser.find_elements_by_css_selector(".detailLink")

for i in range(len(links)):

    links=browser.find_elements_by_css_selector(".detailLink") 

    links[i].click()

    time.sleep(2)

    browser.execute_script("window.history.go(-1)")

    time.sleep(2)


with open('output.csv', "w") as f:

    writer = csv.writer(f)

    writer.writerow(["S.No","Status","Organization","Project Title","First Publishing Date","Work Category","Listing Deadline"])

    table=browser.find_elements_by_xpath("//table[@class='list-table']//tr")

    for row in range(len(table)):

        x=[]

        for d in browser.find_elements_by_xpath("//table[@class='list-table']//tr["+str(row)+"]//td"):

            x.append(d.text.encode('utf-8'))

        writer.writerow(x)



browser.close()


查看完整回答
反對 回復(fù) 2022-06-22
?
海綿寶寶撒

TA貢獻(xiàn)1809條經(jīng)驗(yàn) 獲得超8個贊

是的,因?yàn)槟苿拥较乱豁?,因?yàn)槟牧隧撁?,它無法在上一頁上找到該元素。你可以試試這個


import csv

import requests

import time

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


 browser = webdriver.Chrome(executable_path=r"D:\jewel\chromedriver.exe")


browser.execute_script("window.open('about:blank','tab1');")

browser.switch_to.window("tab1")

browser.get("https://e-sourcingni.bravosolution.co.uk/web/login.shtml")

signInButton = browser.find_element_by_css_selector(".only")

signInButton.click()

time.sleep(5)

table = browser.find_element_by_css_selector(".list-table")


for a in table.find_elements_by_tag_name("a"):

    try:

        if a.get_attribute("class") == "detailLink":

            id = a.get_attribute("onclick")

            id = id.replace("javascript:goToDetail('","")

            id = id.replace("', '02260');stopEventPropagation(event);", "")

            a_href = a.get_attribute("href")

            browser.execute_script("window.open('about:blank','tab2');")

            browser.switch_to.window("tab2")

            browser.get("https://e-sourcingni.bravosolution.co.uk/esop/toolkit/opportunity/opportunityDetail.do?opportunityId="+ id +"&oppList=CURRENT")


            time.sleep(2)

            #wait for the element to load


            browser.switch_to.window("tab1")

            # print("in it ")

    except:

        print("detailLink is not present in the a tag class")


with open('output.csv', "w") as f:

    writer = csv.writer(f)

    writer.writerow(["S.No","Status","Organization","Project Title","First Publishing Date","Work Category","Listing Deadline"])

    for row in table.find_elements_by_css_selector('tr'):

        writer.writerow([d.text for d in row.find_elements_by_css_selector('td')])



browser.close()


查看完整回答
反對 回復(fù) 2022-06-22
  • 2 回答
  • 0 關(guān)注
  • 113 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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