程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python combat]--Batch download Baidu Gallery pictures

編輯:Python

系列文章目錄

文章目錄

  • 系列文章目錄
  • 前言
  • 一、效果展示:
  • 二、使用步驟
    • 1.Copy the source code and modify the path
    • 2. python運行即可!
  • 總結


前言

一、效果展示:

二、使用步驟

1.Copy the source code and modify the path

代碼如下(示例):

from lxml.html import etree
import requests
from PIL import Image
import os
import sys
path = './tutu4'
if not os.path.exists(path):
os.makedirs(path)
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.9',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'
}
url_list = []
area = input('請輸入類別:')
if area.find('美女')>=0:
area = '4kmeinv'
elif area.find('風景')>=0:
area = '4kfengjing'
elif area.find('游戲')>=0:
area = '4kyouxi'
elif area.find('動漫')>=0:
area = '4kdongman'
elif area.find('影視')>=0:
area = '4kyingshi'
elif area.find('明星')>=0:
area = '4kmingxing'
elif area.find('汽車')>=0:
area = '4kqiche'
elif area.find('動物')>=0:
area = '4kdongwu'
elif area.find('人物')>=0:
area = '4krenwu'
elif area.find('美食')>=0:
area = '4kmeishi'
elif area.find('宗教')>=0:
area = '4kzongjiao'
elif area.find('背景')>=0:
area = '4kbeijing'
else:
print('The input keyword format is incorrect')
sys.exit(0)
page = int(input('請輸入頁數:'))
for i in range(0,page+1):
i = int(i)
if i==1:
url = 'http://pic.netbian.com/'+ area +'/index.html'
res = requests.get(url=url,headers=headers)
res.encoding = res.apparent_encoding # 修改編碼
response = etree.HTML(res.text)
response = etree.tostring(response)
response = etree.fromstring(response) # 以上搭建xpath對象
content = response.xpath('//ul[@class="clearfix"]/li')
for i in content:
tu_url = i.xpath('./a/@href')
tupian_url = 'http://pic.netbian.com'+ ''.join(tu_url)
url_list.append(tupian_url)
elif i>=1:
i = str(i)
url = 'http://pic.netbian.com/' + area + '/index_' + i + '.html'
res = requests.get(url=url,headers=headers)
res.encoding = res.apparent_encoding # 修改編碼
response = etree.HTML(res.text)
response = etree.tostring(response)
response = etree.fromstring(response) # 以上搭建xpath對象
content = response.xpath('//ul[@class="clearfix"]/li')
for i in content:
tu_url = i.xpath('./a/@href')
tupian_url = 'http://pic.netbian.com'+ ''.join(tu_url)
url_list.append(tupian_url)
for i in url_list:
r = requests.get(url=i, headers=headers)
r.encoding = r.apparent_encoding # 修改編碼
html = etree.HTML(r.text)
html = etree.tostring(html)
html = etree.fromstring(html) # 以上搭建xpath對象
url = html.xpath(r'//a[@id="img"]/img/@src')
rr = requests.get('http://pic.netbian.com' + ''.join(url), headers=headers)
name = html.xpath(r'//a[@id="img"]/img/@title')
rr.encoding = rr.apparent_encoding # 修改編碼
with open(f"./tutu4/{
''.join(name)}" + '.png', 'wb') as fw:
fw.write(rr.content)
img = Image.open(f"./tutu4/{
''.join(name)}" + '.png')
img = img.resize((4000, 2000), Image.ANTIALIAS) # 改變大小 抗鋸齒
# print(img.size)
img.save(f"./tutu4/{
''.join(name)}" + '.png', quality=95)
print(str(name) + " 保存完成!")
print("保存完成!")

2. python運行即可!

資源傳送門


總結

分享:
寫作是心靈的自我對話一—它有著諸多的約束條件,因此並不是所有的人在任何時段、任何環境下都適合寫作.沒有思想骨架作為支撐,任何文字都只是一種軟體的堆積.對於一個作者而言,什麼時候都可以寫,問題在於想不想寫.


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved