import concurrent import requests; from concurrent.futures import ThreadPoolExecutor import os; import parsel; def send_request(url): header = { "user ...
import concurrent
import requests;
from concurrent.futures import ThreadPoolExecutor
import os;
import parsel;
def send_request(url):
header = {
"user-agent":'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
}
requests.packages.urllib3.disable_warnings()
response = requests.get(url,headers=header)
return response
def pare_data(data):
selector = parsel.Selector(data)
result_list = selector.xpath('//a[@class="col-xs-6 col-sm-3"]')
for result in result_list:
title = result.xpath('./img/@alt').get()
src_url = result.xpath('./img/@data-original'). get()
all_title = title+'.'+src_url.split('.')[-1]
yield all_title,src_url
def save_data(file_name,data):
dir_name = 'doutu_list'
if not os.path.exists(dir_name):
os.mkdir(dir_name)
with open(dir_name+'/'+file_name,'wb') as f:
f.write(data)
print("保存完成:",file_name)
def main(page):
for page in range(1,page+1):
print('########################當前為第{}頁########################'.format(page))
thread_pool = concurrent.futures.ThreadPoolExecutor(max_workers=1)
res = send_request('https://www.doutula.com/photo/list/?page={}'.format(str(page)))
src_url = pare_data(res.text)
for file,url in src_url:
print(file)
print(url)
image_response = send_request(url)
thread_pool.submit(save_data,file,image_response.content)
thread_pool.shutdown()
if __name__=='__main__':
main(1)
高顏值後臺管理系統免費使用 ### 子楓後臺管理系統 ###,可在寶塔面板直接安裝
歡迎關註我的公眾號:子楓的奇妙世界,獲得獨家整理的學習資源和日常乾貨推送。
如果您對我的其他專題內容感興趣,直達我的個人博客:www.wangmingchang.com 。