2014-12-13 2 views
0

그래서 내가 유튜브와 나는이 양식을 얻고 곳의 예에서 썸네일을 끌어 시도하고 여기에 있습니다 :파이썬 XPath는 URL 이미지

https://www.youtube.com/results?search_query=funny

이 때문에 XPath는 무엇을 각 이미지에 대한 링크를 찾아 내가 찾고 있어요,하지만 Python을 통해 그것을 실행하면 "NO image"python이 xpath를 볼 수 없지만 xpath를 볼 수는 없지만 테스트를 수행하면 충돌이 발생한 직후에이를 볼 수 있습니다.

try: 
    thumbnail = browser.find_element_by_xpath("/html/body/div[2]/div[3]/div/div[5]/div/div/div/div[1]/div/div[2]/div[2]/ol/li/ol/li[1]/div/div/div[1]/a/div/img/@src").text 
except NoSuchElementException: 
    print "NO image" 
    thumbnail = 'n/a' 

누구나 아이디어가 있습니까?

+0

복제로 표시를하지만의 중복이었다 질문 더 이상 존재하지 않다. – Jarad

답변

5

는 그 다음 "결과"사업부의 모든 img 태그를 얻기 src 속성 값을 얻을 수 get_attribute()를 사용

from selenium import webdriver 

driver = webdriver.Firefox() 
driver.get('https://www.youtube.com/results?search_query=funny') 

for image in driver.find_elements_by_xpath('//div[@id="results"]//img[@src]'): 
    print image.get_attribute('src') 

driver.close() 

인쇄 :

https://i4.ytimg.com/vi/cKhqUOncefY/mqdefault.jpg 
https://i1.ytimg.com/vi/6-JDfR8x55E/mqdefault.jpg 
https://i.ytimg.com/vi/7KU8leAQat4/mqdefault.jpg 
https://i.ytimg.com/vi/R7ghDhpCLKM/mqdefault.jpg 
https://i.ytimg.com/vi/VxvDVhjALoU/mqdefault.jpg 
... 
+0

목록 # 1에서 가져온 정보를 미리보기 # 1로 표시하면 모든 이미지를 가져 오는 것입니다. – BubblewrapBeast

+0

@BubblewrapBeast, driver.find_element_by_xpath ('// div [@ id = "results"] // img [@src]'). get_attribute ('src')'. – alecxe