Picture preview
Steps
这里以搜狗搜图作为一个简单的例子:
第一步就是获取我们的需求了,输入需要下载的图片名字以及需要下载的图片数量。
接下来我们需要在搜狗搜图中输入用户的需求,透过
x
p
a
t
h
xpath
xpath锁定搜索框,然后输入需求,进入图片浏览页面。
然后我们点击第一张图片,进入另一个窗口,这个时候我们代码中的窗口也需要跟着变换
b
.switch_to
.window
(b
.window_handles
[1])
然后就可以开始我们的爬取了,同样的通过
x
p
a
t
h
xpath
xpath得到我们的下载连接,再一张一张的下载到我们设定好的地址中。
为了方便,我设置了一个默认的地址,即
D
:
/
+
n
a
m
e
D:/ + name
D:/+name的形式文件。
path
= "D://" + name
if not os
.path
.exists
(path
):
os
.mkdir
(path
)
Coding
from selenium
import webdriver
from selenium
.webdriver
.common
.keys
import Keys
import time
import requests
import os
chromedriver_path
= "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"
b
= webdriver
.Chrome
(chromedriver_path
)
def get_page(name
):
b
.get
("https://pic.sogou.com/")
search_window
= b
.find_element_by_xpath
(r
'''//*[@id="form_querytext"]''')
search_window
.send_keys
(name
)
search_window
.send_keys
(Keys
.ENTER
)
time
.sleep
(2)
def start_spider(num
, path
):
new_window
= b
.find_element_by_xpath
(r
'''/html/body/div[2]/div[2]/ul/li[1]/div/a[1]/img''')
new_window
.click
()
b
.switch_to
.window
(b
.window_handles
[1])
for i
in range(num
):
print("正在下载第%d张" % (i
+ 1))
image_element
= b
.find_element_by_xpath
(r
'''//*[@id="imageBox"]/img''')
link
= image_element
.get_attribute
("src")
link
= requests
.get
(link
)
image_path
= path
+ "//%d.jpg" % (i
+ 1)
down_load
(image_path
, link
)
next_page
= b
.find_element_by_xpath
(r
'''//*[@id="btnPgRgt"]/span''')
next_page
.click
()
time
.sleep
(1)
def down_load(path
, link
):
with open(path
, 'wb') as f
:
f
.write
(link
.content
)
time
.sleep
(1)
f
.close
()
if __name__
== "__main__":
name
= input("输入下载图片名称:")
num
= int(input("输入下载图片数量:"))
path
= "D://" + name
if not os
.path
.exists
(path
):
os
.mkdir
(path
)
get_page
(name
)
start_spider
(num
, path
)
print("下载完成")