-
Python 3.6
-
Pycharm
import requests
import parsel
import threading
相关模块 pip 安装即可
这个网站有电脑壁纸也有手机壁纸,还是不错的。
网站是静态网站,没有加密,可以直接爬取
1、先在列表页面获取每张壁纸的详情地址
2、在壁纸详情页面获取壁纸真实高清url地址
3、保存壁纸
模拟浏览器请求网页,获取网页数据
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)
启动多线程运行代码