1.上面写的,我每天的壁纸都是Windows自带的天蓝色。看着真的很无聊。有趣吗?无聊~所以,众所周知,我是一个喜欢高品质的博主,当然有高品质的壁纸,别的不说。好了,不啰嗦了,开始今天的优质之旅吧~2.安排好python3.6pycharmrequestsparsel上的所有准备工作3.爬虫流程1)关于数据源搜索:1.确定目标需求:爬取高清壁纸图片(theothershore)通过开发者工具找到图片url地址的来源(F12或右键查看);请求壁纸详情页获取其网页源码获取图片的url地址(一张);请求列表页获取每个壁纸详情页的url和标题;2)代码实现:1.发送请求壁纸列表页面url:http://www.netbian.com/1920x1...2.获取数据网页源码/response.text网页文本数据3.解析数据cssxpathbs4re壁纸详情页url:/desk/23397.htm2.壁纸标题4.保存数据保存图片是二进制数据观众爷爷:就这?代码呢?不放过代码是什么意思?别慌,来了来了4.我就不一一拆解代码展示了,第三步加在评论里。相信聪明的你都能看懂。如果不行,我会在最后的视频中解释。importrequests#请求模块第三方模块pipinstallrequestsimportparsel#数据分析模块第三方模块pipinstallparselimporttime#时间模块内置模块time_1=time.time()#使用模块首先要知道什么range(2,12)中页面的模块是什么:print(f'======================================================================================================================================')url=f'http://www.netbian.com/1920x1080/index_{page}.htm'#请求头:将python代码伪装成浏览器发送到服务器发送请求头={'User-Agent':'Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/89.0.4389.114Safari/537.36'}response=requests.get(url=url,headers=headers)#出现乱码怎么办?需要转码#html_data=response.content.decode('gbk')response.encoding=response.apparent_encoding#自动转码#获取源代码/获取网页文本数据response.text#print(response.text)#解析数据选择器=parsel.Selector(response.text)#CSS选择器是根据网页标签的内容提取数据#第一次提取提取li标签的所有内容lis=selector.css('.listli')forliinlis:#http://www.netbian.com/desk/23397.htmttitle=li.css('b::text').get()如果标题:href='http://www.netbian.com'+li.css('a::attr(href)').get()response_1=requests.get(url=href,headers=headers)selector_1=parsel.Selector(response_1.text)img_url=selector_1.css('.picimg::attr(src)').get()img_content=requests.get(url=img_url,headers=headers).contentwithopen('img\\'+title+'.jpg',mode='wb')asf:f.write(img_content)print('保存:',title)time_2=time.time()use_time=int(time_2)-int(time_1)print(f'Totaltimespent{use_time}seconds')可以自己跑试试看,连续三连wow
