美文网首页工作生活
fetch colors from color-co

fetch colors from color-co

作者: 狼无雨雪 | 来源:发表于2019-07-15 14:04 被阅读0次


"""
really used in fetching url from google images
"""
import re
from selenium import webdriver
import time
import os
import sys
import re
from bs4 import BeautifulSoup
import random
from selenium.webdriver.chrome.options import Options

down_loading_urls = range(1,50)

if __name__ == "__main__":
    baidu_path = 'color_co'  
    


    temp_path = baidu_path + "/" + "temp_color2.txt"
    path = baidu_path + "/" + "color2.txt"



    # os.environ["PATH"] += os.pathsep + 'D:\google-art-downloader-master'
    if not os.path.exists(baidu_path):
        os.makedirs(baidu_path)
    # option = webdriver.ChromeOptions()
    # option.add_argument('--headless')
    # option.add_argument('--disable-gpu')
    # browser = webdriver.Chrome(chrome_options = option)
    fireFoxOptions = webdriver.FirefoxOptions()
    fireFoxOptions.set_headless()
    browser = webdriver.Firefox(firefox_options=fireFoxOptions)

    asserts_all=list()
    mark_time = 0
    last_value = 0
    now_len = 0
    pre_len = 0
    count_all = 0

    try:
        for down_loading_url in down_loading_urls:
            original_url =  "https://coolors.co/browser/best/{}".format(down_loading_url)
            print("down_loading_url: ",original_url)
            browser.get(original_url)
            time.sleep(25)
            pageSource = browser.page_source
            soup = BeautifulSoup(pageSource,'lxml')
            asserts = soup.find_all('div', {"class":"browser-palette-colors"})
            for lines in asserts:
                for line in lines.children:
                    if line.find("span") != -1:
                        color_value = line.find("span").get_text()
#                         print(color_value)
                        color_value = color_value.strip()
                        if color_value != None and color_value != "":
                            with open(temp_path,'a',encoding="utf-8") as w_file:
                                w_file.write(color_value + "\n")
                            asserts_all.append(color_value)
            now_len = len(asserts_all)
            print("now_len: ",now_len)
            if pre_len == now_len:
                print("error: ", original_url)
            pre_len = now_len

    except Exception as e:
        print("global",e,original_url)
    finally:
        with open(path,'w',encoding="utf8") as write_file:
            for line in asserts_all:
                write_file.write(str(line)+"\n")
    #     pass
        browser.close()

相关文章

网友评论

    本文标题:fetch colors from color-co

    本文链接:https://www.haomeiwen.com/subject/ilxdhctx.html