python爬虫之抓取高清壁纸
发布时间:2023-05-04 08:54:21 339 相关标签: # css# python# 爬虫# 技术
对于很多电脑玩家喜欢给自己电脑装饰精美的壁纸,但是碍于图片大小清晰度等原因无法找到合适的,今天我就利用爬虫技术手段,现实高清壁纸抓取,需要的小伙伴可以一起过来看看!
# coding=utf-8
import os.path
import requests
import parsel
def get_address():
"""
获取url地址
:return:
"""
dirname = 'photo/'
if not os.path.exists(dirname):
os.mkdir(dirname)
for page in range(2, 11):
print(f'=====================正在爬取第{page}页内容========================')
url = f'http://www.netbian.com/1920x1080/index_{page}.htm'
# url = 'http://www.netbian.com/1920x1080/'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36 Edg/99.0.1150.46'
}
res = requests.get(url, headers=headers)
res.encoding = res.apparent_encoding
selector = parsel.Selector(res.text)
href = selector.css('.list li a::attr(href)').getall()
#
url_lis = selector.css('.list li')
for lis in url_lis:
title = lis.css('b::text').get()
# 取出广告页面
if title:
list_url = 'http://www.netbian.com' + lis.css('a::attr(href)').get()
# print(list_url)
res1 = requests.get(list_url, headers=headers)
# print(res1.text)
selector1 = parsel.Selector(res1.text)
img_url = selector1.css('.pic img::attr(src)').get()
# print(img_url)
# 保存图片
img_content = requests.get(url=img_url).content
with open('photo/' + title + '.jpg', 'wb') as f:
f.write(img_content)
print(title, img_url)
get_address()
文章来源: https://blog.51cto.com/u_13488918/5989497
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报