如何从硒中获取img

时间:2016-04-01 11:20:42

标签: python python-2.7 selenium selenium-webdriver

如何使用selenium包获取图像超链接。

from selenium import webdriver 
driver = webdriver.Firefox()
driver.get("http://m.imdb.com/feature/bornondate")
elements = driver.find_elements_by_xpath("//a[@class='poster ']")
li = [["Name","Movie Title","Image"]]
for i in  elements:
    print i.find_element_by_tag_name("img") ##I am not sure how to get the URL
    new_line= i.text.splitlines()
    #print new_line[0] , " " , new_line[1]
    li.append(new_line)

print li 

将数据写入CSV文件

with open ('imdb.csv','wb')as fp:
    a = csv.writer(fp, delimiter=',')
    a.writerows(li)

1 个答案:

答案 0 :(得分:10)

要获取类似src的元素属性,您需要调用get_attribute('attr_name')属性。

您只需将以下代码添加到for循环中:

for i in  elements:
    image = i.find_element_by_tag_name("img")
    img_src = image.get_attribute("src")