Python下载白蛇缘起高清图片

下载相关的包

安装python依赖包

1
2
3
pip install selenium
pip install lxml
pip install requests

下载Chrome

下载地址

1
http://npm.taobao.org/mirrors/chromedriver/

将下载的包放的本地磁盘,命名为chromedriver.exe,然后设置系统环境变量到该目录

Python 代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# -*- coding:utf-8 -*-
# __author: "Farmer"
# date: 2019/03/05
from selenium import webdriver
import time
from lxml import etree
import requests
import os
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome()

# 我用的地址
driver.get("http://tieba.baidu.com/photo/p?kw=%E7%94%B5%E5%BD%B1%E7%A5%A8%E6%88%BF&ie=utf-8&flux=1&tid=6008650642&pic_id=0b8aa8014c086e06b78cbfbc0f087bf40ad1cb31&pn=1&fp=2&see_lz=1")
# 浏览器窗口最大
driver.maximize_window()
time.sleep(5)
links = []
end = 0
while end == 0:
left_click = driver.find_element_by_class_name('carousel_arrow_next')
im = driver.page_source
links.extend(etree.HTML(im).xpath('//img/@src'))
ActionChains(driver).click(left_click).perform()
tag = left_click.get_attribute('style')
if "inherit" in tag:
end = 0
else:
end = 1
time.sleep(10)

headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko)"
" Chrome/64.0.3282.186 Safari/537.36",
"Accept": "text/html,application/xhtml+xml,application/xml; q=0.9,image/webp,*/*;q=0.8"}


# 判断存放目录是否存在,不存在就创建
if not os.path.exists('img'):
os.mkdir('img')


for i in links:
if "about" in i:
pass
html = requests.get('http://imgsrc.baidu.com/forum/pic/item/' + i.split('/')[-1], headers=headers)
name = 'img/' + i.split('/')[-1]
if '.jpg' not in name:
name += '.jpg'
with open(name, 'wb') as f:
f.write(html.content)
# 退出
driver.quit()

执行结果

获取图片

图片(部分)如下

坚持原创技术分享,您的支持将鼓励我继续创作!