编写一个程序,访问图像共享网站,如 Flickr 或 Imgur,查找一个类型的照片, 然后下载所有查询结果的图像。可以编写一个程序,访问任何具有查找功能的图像 网站。
风之动漫是一个在线漫画网站网址是http://www.fzdm.com/
先进入该网站:
点击上面的在线漫画:
点击犬夜叉:
拉到下面点击第1卷,这样就能到达第一卷漫画的地方:
代码:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time, requests, os, bs4
os.makedirs('picture', exist_ok=True)
url = 'http://www.fzdm.com/'
browser = webdriver.Firefox()
browser.get(url)
time.sleep(3)
browser.find_element_by_link_text('在线漫画').click()
browser.find_element_by_link_text('犬夜叉').click()
htmlElem = browser.find_element_by_tag_name('html')
htmlElem.send_keys(Keys.END)
browser.find_element_by_link_text('犬夜叉第1卷').click()
url = browser.current_url
while True:
print('Downloading page %s...' % url)
# Find the url of the image
image = browser.find_element_by_id('mhpic')
imageUrl = image.get_attribute('src')
# Download the image.
print('Downloading iamge %s...' % imageUrl)
res = requests.get(imageUrl)
res.raise_for_status()
# Save the image to ./picture
imageFile = open(os.path.join('picture', os.path.basename(imageUrl)), 'wb')
for chunk in res.iter_content(100000):
imageFile.write(chunk)
imageFile.close()
# Get the Prev button's url
prevButton = browser.find_elements_by_link_text('下一页')
if len(prevButton) == 0:
break
else:
prevButton[0].click()
url = browser.current_url
browser.quit()
结果:
其他的漫画也可以用类似的方法下载