爬取美女图片并保存本地和多线程部分
多线程
import requests
from concurrent.futures import ThreadPoolExecutor
from lxml import html
etree = html.etree
def get_list(url):
etree = html.etree
list_resp = requests.get(url)
list_resp.encoding = "utf-8"
tree = etree.HTML(list_resp.content)
list_gg = tree.xpath('//div[@class="item-media entry"]//a')
with ThreadPoolExecutor(10) as t:
for gg in list_gg[2:16]:
if gg.xpath('./@title')[0] != '': #标题不为空
gg_title = gg.xpath('./@title')[0]
gg_href = "https://www.mn52.com"+gg.xpath('./@href')[0]
print(gg_href,gg_title)
t.submit(get_download,gg_href,gg_title) #多线程
# get_download(gg_href,gg_title)
def get_download(gg_href,gg_title):
etree = html.etree
png_resp = requests.get(gg_href)
png_resp.encoding = 'utf-8'
tree = etree.HTML(png_resp.content)
png_urls = tree.xpath('//div[@id="originalpic"]//img/@src')
number = 1
with ThreadPoolExecutor(10) as t:
for png_url in png_urls:
png_url = "https:" + png_url
png_name = gg_title + str(number)
number =int(number)+1
with open("美女图片/" + png_name+".png", mode="wb") as f:
png_down = requests.get(png_url)
f.write(png_down.content) # 图片内容写入文件
print(f"{png_name}下载成功",)
if __name__ == '__main__':
get_list("https://www.mn52.com/xingganmeinv/")

浙公网安备 33010602011771号