【Python】下载图片
2023-12-26 19:16:03
用Python下载图片
查看Python 版本
python -V
准备下载这个网站图片
用Xpath调试网页图片
直接上代码
# @Time : 2023/12/26 14:57
# @Author : zhangchunyang
# @File : DownloadImage.py
# 下载图片
import urllib.request
from lxml import etree
imgUrl = 'https://www.pexels.com/zh-cn/'
headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-AU;q=0.7,en-CA;q=0.6,en-US;q=0.5',
'Cache-Control': 'max-age=0',
'Cookie': 'remember_user_token=eyJfcmFpbHMiOnsibWVzc2FnZSI6Ilcxc3pOREV6TnpVMVhTd2lKREpoSkRFd0pDOXpUa04zV1cxRGJpNDFja2RXV21obE9IZzRVazhpTENJeE5qZzVNRFU1TlRVekxqTTNPVEF4TmpZaVhRPT0iLCJleHAiOiIyMDI0LTA3LTExVDA3OjEyOjMzLjM3OVoiLCJwdXIiOiJjb29raWUucmVtZW1iZXJfdXNlcl90b2tlbiJ9fQ%3D%3D--d328488ca89fe6e9ed5b8e3dfe2e0ba62ed7c8dd; _hjSessionUser_171201=eyJpZCI6IjZiMDEzMGEzLTY1YTEtNWNlMi04ZDAwLWNkOTE1Y2YxNGYzZCIsImNyZWF0ZWQiOjE2ODkwNTk1MzY5OTMsImV4aXN0aW5nIjp0cnVlfQ==; __cf_bm=5_TkpOVjWcVOC4V2zAi2qrs6zRS.e1YI0GuMUojNvW0-1703577572-1-AdiAXwW6iHC73XmlmPEsNIYSIo4pRzJZcmoQq+aQOlr9HHVlCx6DhNaMDkp/cTmRaDgn1WbeEwW8fBZQH31dHkg=; _sp_ses.9ec1=*; _sp_id.9ec1=5ee3cc49-6c22-4693-9774-3ec4cc2112e0.1689059535.2.1703578290.1689059576.afe5b8f8-3fa2-41fc-83a3-e13741416013.b5c7fd41-97b3-4433-b432-af91e9e136ba.f2a2c0ad-6542-4d03-ba54-07c6da431f74.1703577577888.4; _gid=GA1.2.2006158523.1703578290; _ga_8JE65Q40S6=GS1.1.1703577577.1.1.1703578289.0.0.0; _ga=GA1.1.101628493.1703577578; _gat=1; OptanonConsent=isGpcEnabled=0&datestamp=Tue+Dec+26+2023+16%3A11%3A30+GMT%2B0800+(%E4%B8%AD%E5%9B%BD%E6%A0%87%E5%87%86%E6%97%B6%E9%97%B4)&version=202301.1.0&isIABGlobal=false&hosts=&landingPath=https%3A%2F%2Fwww.pexels.com%2Fzh-cn%2F&groups=C0001%3A1%2CC0002%3A0%2CC0003%3A0%2CC0004%3A0; cf_clearance=EKm1bhNVohYSK82X.kZrzLWawTgNLpJQDfSqHtIhd50-1703578289-0-2-1d716c65.26cb2bb0.35f87120-0.2.1703578289',
'Sec-Ch-Ua': '"Chromium";v="118", "Google Chrome";v="118", "Not=A?Brand";v="99"',
'Sec-Ch-Ua-Mobile': '?0',
'Sec-Ch-Ua-Platform': '"Windows"',
'Sec-Fetch-Dest': 'document',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-Site': 'same-origin',
'Sec-Fetch-User': '?1',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36'
}
request = urllib.request.Request(url=imgUrl, headers=headers)
response = urllib.request.urlopen(request)
content = response.read().decode('"utf-8')
print(content)
tree = etree.HTML(content)
altList = tree.xpath('///div[@class="BreakpointGrid_item__erUQQ"]//a/img/@alt')
srcList = tree.xpath('//div[@class="BreakpointGrid_item__erUQQ"]//a/img/@src')
for i in range(len(altList)):
alt = altList[i]
src = srcList[i]
print("-----alt is : " + alt + "-----src is : " + src)
imgUrl = 'https:' + src
print("-----imgUrl is : " + imgUrl)
urllib.request.urlretrieve(url=imgUrl, filename='D:\\image\\' + alt + '.jpg')
下载完成!!!
文章来源:https://blog.csdn.net/wochunyang/article/details/135224355
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!