2017-02-12 8 views
0

속성 URL에서 URL을 가져 오는 방법은 URL 자체의 스타일을 의미합니까? style = "width : 433px; 높이 : 510px; 배경 이미지! URL (https://cs7056.vk.me/c635104/v635104607/1c316/ADzy-2WY8pw.jpg) "당신을 위해 Selenium3 Python3 쉽게 특정 경우Selenium3 Python3 속성 스타일 = "background-image : ur," "url이 있고 URL이 있습니다"

import requests 
from bs4 import BeautifulSoup 
from selenium import webdriver 
from selenium.webdriver.common.by import By 
from selenium.webdriver.common.keys import Keys 
from selenium.common.exceptions import NoSuchElementException 
from selenium.common.exceptions import NoAlertPresentException 
import re 
import time 


url = 'https://vk.com/uporols_you' 
driver       = webdriver.Firefox(executable_path='C:/Users/PANDEMIC/AppData/Local/Mozilla/geckodriver.exe') 

def login(driver): 
    log_page     = driver.get('https://login.vk.com/?act=login') 
    find_login_input   = driver.find_element_by_id('login_form').find_element_by_id('email').send_keys('+77782303865') 
    find_password_input   = driver.find_element_by_id('login_form').find_element_by_id('pass').send_keys('pass') 
    find_button     = driver.find_element_by_xpath('//button[@id="login_button"]').click() 
    time.sleep(5) 



def get_photo_from_page(driver): 
    driver.get(url) 
    try: 
     driver.find_element_by_class_name('popup_box_container').find_element_by_class_name('box_title_wrap').find_element_by_class_name('box_x_button').click() 
    except: 
     print('nope nothing') 

    for i in range(2): 
     scrol_down = driver.find_element_by_id('public_wall').find_element_by_id('wall_more_link').click() 
     time.sleep(2) 

    tut = [] 
    #t = (a[@class="page_post_thumb_wrap image_cover page_post_thumb_last_column page_post_thumb_last_row"]) 
    for ii in driver.find_elements_by_xpath('//a[@style]'): 
     o = ii.get_attribute('style') 
     print(o) 
    #soup = BeautifulSoup(htlm, 'lxml') 
    #im = soup.find_all('a', class_="'page_post_thumb_wrap image_cover page_post_thumb_last_column page_post_thumb_last_row'") 
    #print(htlm) 
    #for a in im: 
    # s = a.get('data-src_big').split('|')[0] 
    # tut.append(s) 
    #print(tut) 

    #for num, link in enumerate(tut, start=1): 
    # p = requests.get(link) 
    # out = open("img%s.jpg" % (num), 'wb') 
    # out.write(p.content) 
    # out.close() 


def main(): 
    login(driver) 
    get_photo_from_page(driver) 


if __name__ == '__main__': 
    main() 

답변

1

, 당신은 당신이 이미 스크립트를 수집 할 수 있었다 스타일 문자열을 구문 분석 할 수 그냥

. 코드에이 기능을 추가

def parse_style_attribute(style_string): 
    if 'background-image' in style_string: 
     style_string = style_string.split(' url("')[1].replace('");', '') 
     return style_string 
    return None 

을이이 "배경 이미지"는 문자열, 또는 어떤 이미지가없는 경우 None을 반환하는 경우 URL을 추출 간단한 문자열 구문 분석이다

. 16,

그런 다음 코드에서 사용할 수 있습니다 :

links = list() 
for ii in driver.find_elements_by_xpath('//a[@style]'): 
    o = ii.get_attribute('style') 
    links.append(parse_style_attribute(o)) 
links = [link for link in links if link is not None] 
+0

하지만 어떻게이 [없음, 없음, 없음, 없음, 'https://pp.vk.me/c638718/v638718210/1b8a6/Fwbewm68Alg를 제거 할 수 있습니다. jpg ', from this'https://cs7056.vk.me/c635104/v635104607/105ca/AmvyePsSzyM.jpg ', –

+0

목록 이해를 사용하여 없음을 제거 할 수 있습니다. 내 대답의 편집을보십시오. –