from selenium import webdriver
import requests
import time
session = webdriver.Safari()
try:
session.get("http://image.baidu.com/search/index?tn=baiduimage&ps=1&ct=201326592&lm=-1&cl=2&nc=1&ie=utf-8&word=baihe")
except:
# session.close()
#js = 'var q=document.documentElement.scrollTop=100000'
#session.execute_script(js)
#time.sleep(10)
list = session.find_elements_by_tag_name('img')
i=0
headers = {
'User-Agent':"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6)\
AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1.2 Safari/605.1.15"
}
for item in list:
url = item.get_attribute("data-imgurl")
if not url == None:
i+=1
r = requests.get(url,headers = headers)
path = "/Users/yaozhengbing/Desktop/picture/"+str(i)+".jpeg"
print(url)
with open(path,'wb') as f:
f.write(r.content)
f.close()
session.close()
注意点:
不能在获取链接前关闭会话
如何通过selenium动态获取属性值 url = item.get_attribute(“data-imgurl”)
记得加请求头
只能爬取部分图片(大概20张),要全部的不要模拟下滑刷新,以后在更新