小白学爬虫——爬取半次元热门榜top100

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import urllib
import re

def ImgSave(url,n):
    u=urllib.request.urlopen(url)
    data=u.read()
    file=open("F://pic//"+str(n)+".jpg","wb")
    file.write(data)
    file.close()

n=0
driver = webdriver.Chrome()
driver.get("https://bcy.net/login")
elem_name=driver.find_element_by_id('email')
elem_pwd=driver.find_element_by_id('password')
elem_name.send_keys("[email protected]")
elem_pwd.send_keys("fuzhe631123")
driver.find_element_by_xpath('//input[@class="btn_green_w121"]').click()
driver.find_element_by_xpath("//a[@href='/illust']").click()
q=20171021
driver.find_element_by_xpath("//a[@href='/illust/toppost100']").click()
print("正在打印"+q+"的排行榜")
while(q>20171016):
    s=driver.page_source
    pattern=re.compile('work-thumbnail__topBd.*?,re.S)
    imgs=re.findall(pattern,s)

    for i in imgs:
        url='https://bcy.net'+i
        driver.get(url)
        s=driver.page_source
        p = re.compile(', re.S)
        ms = re.findall(p, s)

        for m in ms:
            n=n+1
            ImgSave(m,n)
    q = q - 1
    driver.get("https://bcy.net/illust/toppost100?type=week&date="+str(q))


driver.close()

你可能感兴趣的:(爬虫,selenium)