import threading
import parsel
import requests
def get_html(html_url):
'''
获取网页源代码
:param html_url: 网页url
:return:
'''
response = requests.get(url=html_url, headers=headers)
return response
def get_par(html_data):
'''
把 response.text 转换成 selector 对象 解析提取数据
:param html_data: response.text
:return: selector 对象
'''
selector = parsel.Selector(html_data)
return selector
def download(img_url, title):
'''
保存数据
:param img_url: 图片地址
:param title: 图片标题
:return:
'''
content = get_html(img_url).content
path = '壁纸\\' + title + '.jpg'
with open(path, mode='wb') as f:
f.write(content)
print('正在保存', title)
def main(url):
'''
主函数
:param url: 列表页面 url
:return:
'''
html_data = get_html(url).text
selector = get_par(html_data)
lis = selector.css('.wb_listbox div dl dd a::attr(href)').getall()
for li in lis:
img_data = get_html(li).text
img_selector = get_par(img_data)
img_url = img_selector.css('.wb_showpic_main img::attr(src)').get()
title = img_selector.css('.wb_pictitle::text').get().strip()
download(img_url, title)
end_time = time.time() - s_time
print(end_time)
if __name__ == '__main__':
for page in range(1, 11):
url = 'http://www.deskbizhi.com/min/list-{}.html'.format(page)
main_thread = threading.Thread(target=main, args=(url,))
main_thread.start()