在Python中使用变量后循环[selenium]

时间:2017-11-01 18:39:05

标签: python selenium for-loop

我正在尝试在循环后打印quallity或任何变量,但它无法正常工作。它只在循环之前工作。我怎么用呢? 我想发送get请求以及所有这些细节链​​接。



import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()

for x in range(1, 2):
    driver.get("http://sparo.live/movies?page=%d" % (x))
    text = driver.find_elements_by_xpath("//div[@class='boxALL']/article/a" )
    print (len(text))
    array = []
    info = []
    for link in text:
        links = link.get_attribute("href")
        array.append(links)

    for i in array:    
        driver.get(i)
        title = driver.find_element_by_xpath(".//*[@id='wrapper']/div/div/div[1]/h1")
        english_title = driver.find_element_by_xpath(".//*[@id='wrapper']/div/div/div[1]/h2")
        year = driver.find_element_by_xpath(".//*[@id='wrapper']/div/div/div[1]/div[2]")
        geners = driver.find_element_by_xpath(".//*[@id='wrapper']/div/div/div[5]/div[1]/span[1]")
        description = driver.find_element_by_xpath(".//*[@id='wrapper']/div/div/div[5]/div[9]/p")
        preimg = driver.find_element_by_xpath(".//*[@id='wrapper']/div/div/div[1]/div[1]/img")
        img = preimg.get_attribute("src")
        quallity = driver.find_element_by_xpath(".//*[@id='tab-content1']/div/div[3]/a/div[1]/p[2]")
        pretrailer = driver.find_element_by_xpath(".//*[@id='wrapper']/div/div/div[6]/ul/li/iframe")
        trailer = pretrailer.get_attribute("src")
        prelinks = driver.find_element_by_xpath(".//*[@id='tab-content1']/div/div[3]/a")
        watchonclick = prelinks.get_attribute("onclick")
        watchlink = watchonclick.replace('open', 'location.replace')
        driver.execute_script(watchlink)
        find_div = driver.find_elements_by_xpath("//div[@class='box']/div[2]/a" )
        for div in find_div:
            links_to_watch = div.get_attribute("href")
            break
        print ("quallity: '%s'." % quallity.text)




1 个答案:

答案 0 :(得分:1)

如果要在循环外打印,则必须将结果存储在某处:

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()

quality=[] #edited line

for x in range(1, 2):
    driver.get("http://sparo.live/movies?page=%d" % (x))
    text = driver.find_elements_by_xpath("//div[@class='boxALL']/article/a" )
    print (len(text))
    array = []
    info = []
    for link in text:
        links = link.get_attribute("href")
        array.append(links)

    for i in array:
        driver.get(i)
        title = driver.find_element_by_xpath(".//*[@id='wrapper']/div/div/div[1]/h1")
        english_title = driver.find_element_by_xpath(".//*[@id='wrapper']/div/div/div[1]/h2")
        year = driver.find_element_by_xpath(".//*[@id='wrapper']/div/div/div[1]/div[2]")
        geners = driver.find_element_by_xpath(".//*[@id='wrapper']/div/div/div[5]/div[1]/span[1]")
        description = driver.find_element_by_xpath(".//*[@id='wrapper']/div/div/div[5]/div[9]/p")
        preimg = driver.find_element_by_xpath(".//*[@id='wrapper']/div/div/div[1]/div[1]/img")
        img = preimg.get_attribute("src")
        quallity = driver.find_element_by_xpath(".//*[@id='tab-content1']/div/div[3]/a/div[1]/p[2]")
        quality.append(quallity.text)     #edited line
        pretrailer = driver.find_element_by_xpath(".//*[@id='wrapper']/div/div/div[6]/ul/li/iframe")
        trailer = pretrailer.get_attribute("src")
        prelinks = driver.find_element_by_xpath(".//*[@id='tab-content1']/div/div[3]/a")
        watchonclick = prelinks.get_attribute("onclick")
        watchlink = watchonclick.replace('open', 'location.replace')
        driver.execute_script(watchlink)
        find_div = driver.find_elements_by_xpath("//div[@class='box']/div[2]/a" )
        for div in find_div:
            links_to_watch = div.get_attribute("href")
            break
        for quality_type in quality:
            print(quality_type)

输出:

HD
HD
HD
HD
HD
HD
HD
HD
HD
BDRip