Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
230 views
in Technique[技术] by (71.8m points)

python - How can i iterate over links in a table with selenium and append them to a list

Im working on a program that automatically downloads charts from the FAA website. here's the table im trying to scrape off of

the link to the actual website: https://www.faa.gov/air_traffic/flight_info/aeronav/digital_products/dtpp/search/results/?cycle=2014&ident=kiah

here's a block of python code:

try:
    links = driver.find_elements_by_xpath("//table[@id = 'resultsTable']//tbody/tr/td/a")

    names = []
    dlinks = []

    for element in links:
        names.append(element.text)
        dlinks.append(element.get_attribute("href"))


    for printNames in names:
        print("Name-" + printNames)

        for printDlinks in dlinks:
            print("Link-" + printDlinks)
            break

But whenever I run the program dlinks.append(element.get_attribute("href")) seems to only append the first link in the html table for every iteration while the element.text does what it's supposed to do.

a piece of the output: Name-TAKEOFF MINIMUMS Link-http://aeronav.faa.gov/d-tpp/2014/sc5to.pdf#nameddest=(IAH)

Name-ALTERNATE MINIMUMS Link-http://aeronav.faa.gov/d-tpp/2014/sc5to.pdf#nameddest=(IAH)

Name-LAHSO Link-http://aeronav.faa.gov/d-tpp/2014/sc5to.pdf#nameddest=(IAH)

Name-DOOBI TWO (RNAV) Link-http://aeronav.faa.gov/d-tpp/2014/sc5to.pdf#nameddest=(IAH) same exact link for all of them

Whenever I replace dlinks.append(element.get_attribute("href")) with print(element.get_attribute("href")) in the for loop it works perfectly fine at iterating over all the links but theres only a problem whenever try to append the result to a list.

result when made the change above:

http://aeronav.faa.gov/d-tpp/2014/sc5to.pdf#nameddest=(IAH) http://aeronav.faa.gov/d-tpp/2014/sc5alt.pdf#nameddest=(IAH) http://aeronav.faa.gov/d-tpp/2014/sc5lahso.pdf#nameddest=(IAH) it printed but didn't append


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It seems like the break command resets the 2nd loop in the nested for loop causing it to display the same link over and over.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...